reading from a txt file

Gary Herron gherron at digipen.edu
Thu Nov 26 16:01:55 EST 2015


On 11/26/2015 12:34 PM, vincentypedro at gmail.com wrote:
> Hey, I'm wondering how to read individual strings in a text file.  I can read a text file by lines with .readlines() ,
> but I need to read specifically by strings, not including spaces.  Thanks in advance

Read the lines with readlines(), as you say, then split each line into 
whatever pieces you want with split (or with any of the many other 
string methods).

A minimal example (without readlines):
 >>> lines = ['a b c', 'aa bb cc']
 >>> for line in lines:
...   words = line.split()
...   print(words)
...
['a', 'b', 'c']
['aa', 'bb', 'cc']


-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418




More information about the Python-list mailing list