Hello, How may I read a text file word by word. All documentation I have seen so far refers to reading line and full documents scans. I have a text file with ordered records of name, surname and a key. How do I pickup the key for further processing?
Reading text file, word by word in pythonManipulating text in Python
Page 1 of 1
3 Replies - 59822 Views - Last Post: 20 November 2006 - 09:25 AM
Replies To: Reading text file, word by word in python
#2
Re: Reading text file, word by word in python
Posted 10 October 2006 - 12:24 PM
Once you've read a line into a string, you can use the split or rsplit functions using a space as a delimiter to return a list of the individual words from that line. You could also read the whole file into one string, and do the split.
#11
Re: Reading text file, word by word in python
Posted 15 November 2006 - 12:27 AM
f = open(filename,"r");
lines = f.readlines();
for i in lines:
thisline = i.split(" ");
#this line is an array of words in a given line. you have split the line which #was a string into an array of words cuz you split the string every time you encountered a space.
lines = f.readlines();
for i in lines:
thisline = i.split(" ");
#this line is an array of words in a given line. you have split the line which #was a string into an array of words cuz you split the string every time you encountered a space.
#27
Re: Reading text file, word by word in python
Posted 20 November 2006 - 09:25 AM
f = open('myfile.txt', 'r')
all_words = map(lambda l: l.split(" "), f.readlines())
Then iterate through all_words.
-Sam
all_words = map(lambda l: l.split(" "), f.readlines())
Then iterate through all_words.
-Sam
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|