1

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?

2
  • If the 'To' and 'title' are always on their own line with no internal linebreaks, you could just split on \n, where the first element is email, second title, and do '\n'.join( therest ) for the body? Commented Apr 18, 2012 at 13:42
  • Maybe you can just read the file line by line. Commented Apr 18, 2012 at 13:45

4 Answers 4

4

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!

Sign up to request clarification or add additional context in comments.

Comments

4

@Hooked gave a good answer for Python2. Since in Python3 * works also for tuple unpacking, you can do:

f_email, f_title, *f_description = tokens

The details are in PEP 3132

3 Comments

Arg, I was just about to post this ;) +1, it's the nice solution for 3.x users.
for completeness, you can join the elements of f_description with '\n's to regain the original description string.
Also, the solution in this spirit, can be adapted to Python2, by passing *tokens to a method which does the processing.
1

If you don't want to use the text as a whole, and are not under 3.x to use the nice splat unpacking, you can simply do it like this:

email = None
title = None
description = ""
with open("test.txt", "r") as f:
    for number, line in enumerate(f):
       if number == 0:
           email = line.strip()
       elif number == 1:
           title = line.strip()
       else:
           description += line

1 Comment

+1: Thank you very helpful but the code can look a bit long if I have a very long string from a file.
1

When you use message.split('\n', 2) you get three parts: first line, second line and remaining lines in one.

Use this form:

f = open("file.txt")  
f_email, f_title, f_description = f.read.split('\n', 2)  
f.close()

Or this:

f = open("file.txt")  
f_email = f.readline()  
f_title = f.readline()  
f_description = f.read()  
f.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.