[Tutor] Parsing text files

Alan Gauld alan.gauld@freenet.co.uk
Tue, 26 Sep 2000 13:25:46 +0000


Re parsing text files.

1st AWK is even better for the sort of thing 
you want to do. If you have it available give it a whirl.

AWK reads each line and splits it into numbered fields for you: 
$0 = whole line
$1 = 1st field
$2...$NF = fields 2 up to the last field

Now in Python you can do something similar with the 
string.split() function or for more power try the 
re.split() function which uses a regex as a field 
separator... something like:

for line in file.readlines():
	fields = line.split()
      total = total + fields[5]  # or whichever dir field you need

Alan G.