Welcome to Dream.In.Code
Become an Expert!

Join 137,398 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,103 people online right now. Registration is fast and FREE... Join Now!




Reading text file, word by word in python

 
Reply to this topicStart new topic

Reading text file, word by word in python, Manipulating text in Python

Chits01
10 Oct, 2006 - 06:53 AM
Post #1

New D.I.C Head
*

Joined: 10 Oct, 2006
Posts: 1


My Contributions
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?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Reading Text File, Word By Word In Python
10 Oct, 2006 - 11:24 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

velcrobandits
RE: Reading Text File, Word By Word In Python
14 Nov, 2006 - 11:27 PM
Post #3

New D.I.C Head
*

Joined: 13 Nov, 2006
Posts: 6


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

samuraisam
RE: Reading Text File, Word By Word In Python
20 Nov, 2006 - 08:25 AM
Post #4

New D.I.C Head
*

Joined: 20 Nov, 2006
Posts: 1


My Contributions
f = open('myfile.txt', 'r')
all_words = map(lambda l: l.split(" "), f.readlines())

Then iterate through all_words.

-Sam
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 03:05AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month