Monday, 3 June 2013

Parsing a fixed width data file

This will need some serious work to be a functioning tool, but some of you will get the idea of what is needed...  Original source is here


ParsingDataDemo.py

A demo file to parse data which is quasi-fixed width

File and script must reside in the same folder...fix this if you want

'''
import sys, os

data_path = (os.path.dirname(sys.argv[0]) + "/").replace("\\","/")  #can be skipped if you follow
data_file = data_path + "ParsingDataDemoData.txt"    #fix this or better still create a tool
#data_file should be sys.argv[1] which allows a user to select a file in a folder
a_file = open(data_file)
data = a_file.readlines()
for a_line in data:
  record_type = a_line[:3]
  parcel_num = a_line[3:11]
  the_rest = a_line[11:]
  print record_type, parcel_num, the_rest

Note it relies solely on the sys and os python modules

No comments:

Post a Comment