[Tutor] Import multiple lines of text into a variable
bob gailer
bgailer at gmail.com
Mon Apr 11 23:24:05 CEST 2011
On 4/11/2011 5:14 PM, Sean Carolan wrote:
>> So right now my code looks something like this:
>>
>> for line in open('myfile','r'):
>> if line.startswith('notes'):
>> ## Assign rest of file to variable
>>
>> Is there an easy way to do this? Or do I need to read the entire file
>> as a string first and carve it up from there instead?
> I ended up doing this, but please reply if you have a more elegant solution:
>
> if line.startswith('notes'):
> break
> notes = open('myfile','r').read().split(notes:\n')[1]
Seems like an elegant solution to me, as long as the file fits available
memory. There will be 2 copies of the file after the split.
Another way:
textFile = open('myfile','r')
for line in textFile:
if line.startswith('notes'):
notes = textFile.read()
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list