pythontextiteration

Iterating over full text lines instead of characters


I noticed when I try to iterate over a file with lines such as "python" "please" "work" I only get individual characters back, such as, "p" "y" "t"...

how could I get it to give me the full word? I've been trying a couple hours and can't find a method. I'm using the newest version of python. Edit: All the quotation marks are new lines.


Solution

  • You can iterate over a file object:

    for line in open('file'):
        for chr in line:
            do_stuff(chr)
    

    See the docs for the details: http://docs.python.org/2/library/stdtypes.html#bltin-file-objects