Reputation: 1793
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?
Upvotes: 1
Views: 4136
Reputation: 166
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()
Upvotes: 1
Reputation: 88987
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
Upvotes: 1
Reputation: 22690
@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
Upvotes: 4
Reputation: 88128
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!
Upvotes: 4