Knowledge Base‎ > ‎Programming‎ > ‎Python‎ > ‎Python Quick Start‎ > ‎

Reading Text Files

Assume you have to read in a parse the data from the DWITE contest question that looks like this:
Sample Input (first 3 shown): 
28 10 1997
27 10 1997 

26 11 1997




The input file DATA1.txt will contain 5 lines, each having 3 integers representing a person's birth date, in a form of DD MM YYYY (separated by spaces).

If you need some sample data you can just create a file by using Notepad (on Windows, or Textedit on Mac) and save as a text file. 

Save it to the same location as the Python program you will write (otherwise you need to use a full path to find the file - check the file's properties to figure this out - watch out for backslashes though "\" - you need to change these to forward slashes).


What you want to be able to do is: 

  • open a link to the file 
  • read the file from disk 
  • look at each line separately
  • split up the data on each line.

Example Code

Look at the following code. You can grab the code below here:


# open the data file read only and attach it to the file handle data

data = open('DATA1.txt')

print data.readline()


# we will loop through the lines in the file below but we could also do that using readline

print data.readline()



# iterate through the data

for line in data:


 print line


 # split is a string method which breaks the line up on the spaces and creates a list with the data

 fields = line.split(" ")

 print fields

 # ['28', '10', '1997\n']


 # Note the \n - a new line character - in the above output

 # You may or may NOT have this new line depending on how the file was created. 

# we want to remove the new line at the end so we call .strip() before we split the line


 fields = line.strip().split(" ")

 print fields



Watch out for things like newlines or other white space characters that you might not expect. Calling .strip on a string will remove these things for you - see

Split is a string method which will take a string a list from the data by splitting it on a given seperator. By default the seperator is any whitespace. See