tags:

views:

30

answers:

4

How would I do this? I want to iterate through each word and see if it fits certain parameters (for example is it longer than 4 letters..etc. not really important though).

The text file is literally a rambling of text with punctuation and white spaces, much like this posting.

+2  A: 

Try split()ing the string.

f = open('your_file')
for line in f:
    for word in line.split():
        # do something

If you want it without punctuation:

f = open('your_file')
for line in f:
    for word in line.split():
        word = word.strip('.,?!')
        # do something
Amber
Removing punctuation may be helpful - `for word in re.sub('[^\w]+', ' ', paragraph).split():`
Seth
I am doing exactly that right now, and for the #do something I am printing the word - but nothing is printing for some reason
ahh never mind, it works! thank you Amber
Seth thanks, that helps as the files I have do have punctuation
A: 

You can simply content.split()

Michał Niklas
A: 

   f = open(filename,"r");
   lines = f.readlines();
   for i in lines:
   thisline = i.split(" ");

FosterZ
A: 
data=open("file").read().split()
for item in data:
   if len(item)>4:
      print "longer than 4: ",item
ghostdog74