[Tutor] Help? Making Variables out of the header row in a CSV file

Luke Paireepinart rabidpoobear at gmail.com
Wed Jun 21 04:06:58 CEST 2006

Ralph H. Stoos Jr. wrote:
> All,
> Please forgive the long question, but duty calls me to get something
> done with Python that I don't want somebody to do with Microsloth Excel or Miserable
> Basic.
I am sympathetic to your cause.
> The part I can't seem to get going has to do with making variables out
> of the header row in the CSV file.  The way it needs to work is that the
> first row may be of differing lengths (extra fields).  I want to have
> the standalone Python script to make the variables and then allow the user
> to sort the data based on the contents of the columns (i.e. pull out the
> records that match one or more criteria).
f = file("FOO.CSV","r")
txt = f.readlines()

headers = txt[0].strip().split(",")
infotable = {}
for x in range(len(headers)):
    infotable[headers[x]] = [y.split(",")[x].strip() for y in txt]
print infotable
#end of test.py

{'PurgeModId': ['6', '6'], 'SW': ['RV0.6.5.27', 'RV0.6.5.27'], 'JobID': 
['392', '408'], 'Machine': ['1125785731', '1125785731'], 'Width': 
['279400', '279400'], 'CoatingFront': ['none', 'none'], 'AtreeNodeID': 
['1228', '75'], 'Finish': ['regular', 'regular'], 'PurgeSuccess': ['N', 
'Y'], 'PPM': ['120', '120'], 'additional_info': ['', ''], 'CoatingBack': 
['none', 'none'], 'PropID': ['44366', '69206'], 'Full_cfg': 
['_SFM20_IOT7_SFM7_BFM20_BFM2', '_SFM20_IOT7_SFM7_BFM20_BFM2'], 
'MediaID': ['527', '29'], 'PurgeStarted': ['Y', 'N'], 'NameJobCfg': 
['_SFM20_IOT7_SFM7_BFM20_BFM2', '_SFM20_IOT7_SFM7_BFM20_BFM2'], 
'PurgeStopModId': ['5', ''], 'plex': ['Simplex', 'Duplex'], 'Grain': 
['y', 'y'], 'debuglog': ['DebugMsgLog.2006_05_24.07_48_00', 
'DebugMsgLog.2006_05_31.14_33_25A'], 'ModIdJobCfg': ['_2_3_4_5_6', 
'_2_3_4_5_6'], 'Weight': ['75', '75'], 'Color': ['white', 'white'], 
'PurgeCauseFaultID': ['927', '1003'], 'Caliper': ['104', '104'], 
'Height': ['431800', '431800'], 'PurgePrepared': ['Y', 'Y'], 
'FinisherCfg': ['DUAL_BFM', 'DUAL_BFM'], 'Drilled': ['FALSE', 'FALSE']}

> The final product would allow me to select the input file, specify some thing
> like the value of the "PPM" or "Grain" (and combinations thereof), and
> write the output to a new CSV file.  The last bit would be some math on the contents of a couple of the fields.
You mean you want to be able to make a new CSV file using the contents 
of the old one, or create an entirely new one using just stuff you need?
Please clarify here.
> Ralph

More information about the Tutor mailing list