0

I'm trying to download filtered geojson data i've downloaded from simplemaps.com into a CSV file from google colab. However I keep getting an attribute error:'list' object has no attribute 'to_csv'. Code below. Any help would be greatly appreciated.

list_of_geocoders=[]
for i in tqdm(all_cities):
  geo=geocoder.osm(i,maxRows=2) #maxRow =2 means it will pick first 2 results for each city, in case the first result is not of type = 'administrative'
  for g in geo:
    if g.json['type'] == 'administrative' :
      if g.json['address'] not in list_of_geocoders: #avoid repetition
        list_of_geocoders.append(g.json['address'])

from google.colab import drive

drive.mount('/content/drive')
path = '/content/drive/My Drive/outpur.csv'

with open(path, 'w', encoding = 'utf-8-sig') as f:
  list_of_geocoders.to_csv(f) 
2
  • to_csv() is a pandas dataframe method. list_of_geocoders is not a dataframe, it's a list of strings (that's what I'm assuming g.json['address'] is). Commented Jun 17, 2022 at 0:31
  • You can use the csv module to write a CSV file by looping over a list. But if each element of the list is just a single string, just write them all to the file. There's nothing to separate with commas, so you don't need to use CSV-specific code. Commented Jun 17, 2022 at 0:32

1 Answer 1

1

to_csv() is for writing a pandas dataframe to a CSV fgile.

If you have a list of strings, just write each string to the file directly.

with open(path, 'w', encoding = 'utf-8-sig') as f:
    for address in list_of_geocoders:
        f.write(f'{address}\n')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.