Inserting column name headers into a Python list -
i trying insert column headers python list. csv file using not have column names , data separated commas. trying write program don't have insert column names csv file every time make weekly maps in arcgis. turn csv dictionary if needed. either way fine. have code far produces dataset useable. know how reference columns in dictionary column name. same method doesn't seem work inserting column names column position.
import os os.chdir('c:\users\####\desktop') filedata = open('south_06_01_2015_secfile.csv','rb') def dataheader(): headerline = filedata.readline() valuelist = headerline.split(',') yield valuelist def databody(): line in filedata.readlines(): segmentline = line.split(',') yield segmentline[0:6] def dataprinter(): gen1 = dataheader() gen2 = databody() print'\n' row in gen2: print row dataprinter()
assuming, user inserts names of columns , csv file not have column names, should job.
def dataheader(columns_no): columns = [] in range(columns_no): text = raw_input("enter column #%d name> " % i) columns.append(text) return columns def databody(columns_no): segmentlines = [] line in filedata.readlines(): segmentline = line.split(',') segmentlines.append(segmentline[:columns_no]) return segmentlines def dataprinter(): columns_no = 6 gen1 = dataheader(columns_no) gen2 = databody(columns_no) print ','.join(gen1) row in gen2: print ','.join(row) dataprinter()
Comments
Post a Comment