[Tutor] How do I go from a textFile.txt to a list [] AND from a textFile.txt to a dictionary of dictionaries?
Only my second try to post to this list. This is a dumb question with a simple (I presume) solution... but... how do I take a txt file with semicolon separated values and create a dictionary and a list? ( in 2.6 because I'm working with ArcGIS.) text file is as follows: (saved as text_data.txt) (it's longer, this is just a sample) ObjID;Xval;Yval;LineID;PolyID 0;1279027.0;246427.9375;0;1 1;1279069.625;246433.234375;0;1 2;1279091.0;246435.046875;1;1 3;1279112.5;246436.3125;1;1 4;1279134.0;246437.0;2;1 5;1279176.875;246436.71875;2;1 6;1279198.375;246435.734375;3;1 7;1279219.75;246434.1875;3;1 I'd like to create a list from said text data... and a dictionary of dictionaries from said data. Eventually I will create a point, line and polygon shapefiles from this file. With the data above, it would be an 8 point pnt file, a 3 line line file and 1 polygon. (point 7 connecting back to point 0) Right now, I'm just trying to get started by creating a list and a dictionary (to see which I want to use when creating my shapefiles.) And, I need to do it in 2.6 because I'm working with ArcGIS. Thanks in advance for your thoughts? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do I go from a textFile.txt to a list [] AND from a textFile.txt to a dictionary of dictionaries?
On 22/07/2012 23:33, Gregory Lund wrote: Only my second try to post to this list. This is a dumb question with a simple (I presume) solution... but... how do I take a txt file with semicolon separated values and create a dictionary and a list? ( in 2.6 because I'm working with ArcGIS.) text file is as follows: (saved as text_data.txt) (it's longer, this is just a sample) ObjID;Xval;Yval;LineID;PolyID 0;1279027.0;246427.9375;0;1 1;1279069.625;246433.234375;0;1 2;1279091.0;246435.046875;1;1 3;1279112.5;246436.3125;1;1 4;1279134.0;246437.0;2;1 5;1279176.875;246436.71875;2;1 6;1279198.375;246435.734375;3;1 7;1279219.75;246434.1875;3;1 I'd like to create a list from said text data... and a dictionary of dictionaries from said data. Eventually I will create a point, line and polygon shapefiles from this file. With the data above, it would be an 8 point pnt file, a 3 line line file and 1 polygon. (point 7 connecting back to point 0) Right now, I'm just trying to get started by creating a list and a dictionary (to see which I want to use when creating my shapefiles.) And, I need to do it in 2.6 because I'm working with ArcGIS. Thanks in advance for your thoughts? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Your starter for 10 is the csv module specifically http://docs.python.org/library/csv.html#csv-fmt-params -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do I go from a textFile.txt to a list [] AND from a textFile.txt to a dictionary of dictionaries?
Hi Greg, Try this (next time try solving yourself first and post what you've done and specific problems you're having trouble with.): import csv # read data using csv reader... fileobj = open('text_data.txt') csvreader = csv.reader(fileobj, delimiter=';') csvreader.next() # skip header # store lines indexed by line id in dict called polylines # lines themselves are represented by lists. polylines = {} # store polygons made up of lines in dict called polygons polygons = {} # loop through point data from csvreader and unpack into # dicts/lists for line in csvreader: #unpack lineid and polyid lineid = line[3] polyid = line[4] # get the point data as list by slicing # first entry in list is point id. # maybe make points into a dict as well? point = line[0:3] # store the point in the correct line list: # first create an empty list if needed then append polylines[lineid] = polylines.get(lineid, []) polylines[lineid].append(point) # store the line in the correct polygon dict: # first create empty dict if needed then add # if the line not already part of the polygon. polygons[polyid] = polygons.get(polyid, {}) if not lineid in polygons[polyid]: polygons[polyid][lineid] = polylines[lineid] # check the results with some pretty printing: import pprint pp = pprint.PrettyPrinter(indent=4) print lines read from csv file: pp.pprint(polylines) print polygons read from csv file: pp.pprint(polygons) Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor