Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7829831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:55:58+00:00 2026-06-02T10:55:58+00:00

I have a string: f = open(file.txt, r) message = f.read() print message >>>

  • 0

I have a string:

f = open("file.txt", r)
message = f.read()

print message
>>> "To: email\ntitle: add title here\nDescription: whatever here\n"

I can split the string by doing:

f_email, f_title, f_description, blank = message.split('\n')

But the problem arises when I have the message like this:

"To: email\ntitle: add title here\nDescription: first line\nSecond line\nthirdline\n"

When I split the string it splits the description as well. I have tried:

f_email, f_title, f_description, blank = message.split('\n',4)

But that obviously returns ValueError because it is splitting more 4 \n’s.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:55:59+00:00Added an answer on June 2, 2026 at 10:55 am

    When you run .split('\n') you return a list. Rather than assign the variables when you split, you can pull them out of the list:

    tokens = message.split('\n')
    f_email = tokens[0]
    f_title = tokens[1]
    f_description = tokens[2]
    

    This can be made less fragile by checking the size of the list. If you know it needs at least three elements, you can:

    assert(len(tokens)>=3)
    

    Another way to get around this is to wrap the thing up in a try/except block:

    tokens = message.split('\n')
    try:  
        f_description = tokens[2]
    except:
        f_description = None
    

    That way you can handle the case for a shorter list the exact way you like!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .txt file looking like this: 1 Some string Some other string
Some open source libraries have tendency to re implement basic structures like string, list,
i have the next code: std::string line; std::ifstream myfile (text.txt); if (myfile.is_open()) { while
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
Hi all i have written a small code to open a particular text file.
I have problem with streaming. When I send small file like 1kb txt everything
I have a txt plain file that has a list of numbers. One number
I have rjecnik.txt file that looks like this mate sime, jure stipica gujo, prvi
suppose i have the following in a text file (or dired buffer) open in
I want to open file, check if string deosn't exist in file write it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.