python - Pasrse API Response CSV to List without writing to file -


goal:

  • directly read .csv api response python list

i using census bureau's bulk geocoder lat/long of many addresses. documentation batch geocoding on page 5-6.

i able read csv list without first saving file.

my first attempt following:

get response:

    import requests     import csv      url = 'http://geocoding.geo.census.gov/geocoder/locations/addressbatch'     payload = {'benchmark':'public_ar_current', 'vintage':'current_current', 'returntype':'locations'}     files = {'addressfile':('addresses.csv',open(tmp_file,'rb'),'text/csv')}     response = requests.post(url,data=payload,files=files) 

handle response (without writing file):

    reader = csv.reader(response.content)     tmp_list = list(reader)      print(tmp_list) 

the output 1-d list:

[[unique_id], [input_address], [match/no_match], [exact/non-exact], [output_address], [lat/long], [tiger_line_id], [tiger_line_side], [ ], .... ]

the newline being read element put list (shown [ ]).

however, if following:

handle response (first writing response file):

    open('out.csv','w') f:         f.write(response.content)      open('out.csv','rb') r:         reader = csv.reader(r)         tmp_list = list(reader)      print(tmp_list) 

the output method desired 2-d list of lists:

[[unique_id, input_address, match/no_match, exact/non-exact, output_address, lat/long, tiger_line_id, tiger_line_side], .... ]

how can read .csv response directly list? avoid i/o operations because may performing 300+ batches @ time (which 300+ write file/read file.

the csv.reader class accepts iterable of lines; if split content lines before passing in should work:

reader = csv.reader(response.content.split('\n')) 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -