Python: reading fields of a CSV listbox

I'm just wondering how I can read a special field from a CVS file with the following structure:

40.0070222,116.2968604,2008-10-28,[["route"], ["sublocality","political"]] 39.9759505,116.3272935,2008-10-29,[["route"], ["establishment"], ["sublocality", "political"]] 

way to read cvs files I worked with:

 with open('routes/stayedStoppoints', 'rb') as csvfile: spamreader = csv.reader(csvfile, delimiter=',', quotechar='"') 

The problem with this is the first 3 fields that I cannot use:

 for row in spamreader: 

line [0], line [1], line [2], I can access without problems. but in the last field, and I think that with csv.reader (csvfile, delimiter = ',', quotechar = '"') is also split for each sub-list:

so when I tried to access, just show me:

 [["route"] 

Anyone who has a solution to handle the last field has a complete list (the list is valid)

 [["route"], ["sublocality","political"]] 

to have access to each category.

thanks

+4
source share
3 answers

Your format is close to json. You just need to wrap each line in brackets and indicate the dates. For each line l just do:

 lst=json.loads(re.sub('([0-9]+-[0-9]+-[0-9]+)',r'"\1"','[%s]'%(l))) 

leads to lst

 [40.0070222, 116.2968604, u'2008-10-28', [[u'route'], [u'sublocality', u'political']]] 

You need to import json parser and regular expressions

 import json import re 

edit : you asked how to access the element containing the "route". answer

 lst[3][0][0] 

"political" is in

 lst[3][1][1] 

If the strings ("political" and others) can contain strings similar to dates, you should go with @unutbu's solution

+2
source

Use line.split(',', 3) to split only the first 3 commas :

 import json with open(filename, 'rb') as csvfile: for line in csvfile: row = line.split(',', 3) row[3] = json.loads(row[3]) print(row) 

gives

 ['40.0070222', '116.2968604', '2008-10-28', [[u'route'], [u'sublocality', u'political']]] ['39.9759505', '116.3272935', '2008-10-29', [[u'route'], [u'establishment'], [u'sublocality', u'political']]] 
+2
source

This is not a valid CSV file. The csv module will not be able to read this.

If the string structure is always like this (two numbers, a date and a nested list), you can do this:

 import ast result = [] with open('routes/stayedStoppoints') as infile: for line in infile: coord_x, coord_y, datestr, objstr = line.split(",", 3) result.append([float(coord_x), float(coord_y), datestr, ast.literal_eval(objstr)]) 

Result:

 >>> result [[40.0070222, 116.2968604, '2008-10-28', [['route'], ['sublocality', 'political']]], [39.9759505, 116.3272935, '2008-10-29', [['route'], ['establishment'], ['sublocality', 'political']]]] 
+2
source

Source: https://habr.com/ru/post/1486496/


All Articles