Scanning a file character by character

Rhodri James rhodri at wildebst.demon.co.uk
Thu Feb 12 22:56:20 EST 2009


On Fri, 13 Feb 2009 03:24:21 -0000, Spacebar265 <spacebar265 at gmail.com>  
wrote:

> On Feb 11, 1:06 am, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
>> Steven D'Aprano <ste... at REMOVE.THIS.cybersource.com.au> wrote:
>> > On Mon, 09 Feb 2009 19:10:28 -0800, Spacebar265 wrote:
>>
>> >> How would I do separate lines into words without scanning one  
>> character
>> >> at a time?
>>
>> > Scan a line at a time, then split each line into words.
>>
>> > for line in open('myfile.txt'):
>> >     words = line.split()
>>
>> > should work for a particularly simple-minded idea of words.
>>
>> Or for a slightly less simple minded splitting you could try re.split:
>>
>> >>> re.split("(\w+)", "The quick brown fox jumps, and falls  
>> over.")[1::2]
>>
>> ['The', 'quick', 'brown', 'fox', 'jumps', 'and', 'falls', 'over']
> Using this code how would it load each word into a temporary variable.

Why on earth would you want to?  Just index through the list.


-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list