简体   繁体   中英

Index Error when Importing A CSV File in Python 3.5

First time to use Python coming from R. I'm trying to reverse geocode a batch of GPS coordinates using reverse-geocoder . The problem is i can't seem to import my CSV file.

My CSV file looks like this:

"lat,lon"  
"14.5553,121.01806"   
"14.60584,120.99963" 
"14.5625,121.02938"

The code which i essentially just copy-pasted:

import csv
input_filename = '/Users/FreeSys/desktop/pyrevtest.csv'
output_filename = '/Users/FreeSys/desktop/pyrevtestcoded.csv'
cities = [(row[0],row[1]) for row in csv.reader(open(input_filename,'rt'),delimiter='\t')]

pp.pprint(cities[0:5])

IndexError                                Traceback (most recent call last)
<ipython-input-8-317fda8cb105> in <module>()
      2 input_filename = '/Users/FreeSys/desktop/pyrevtest.csv'
      3 output_filename = '/Users/FreeSys/desktop/pyrevtestcoded.csv'
----> 4 cities = [(row[0],row[1]) for row in csv.reader(open(input_filename,'rt'),delimiter='\t')]

<ipython-input-8-317fda8cb105> in <listcomp>(.0)
      2 input_filename = '/Users/FreeSys/desktop/pyrevtest.csv'
      3 output_filename = '/Users/FreeSys/desktop/pyrevtestcoded.csv'
----> 4 cities = [(row[0],row[1]) for row in csv.reader(open(input_filename,'rt'),delimiter='\t')]

IndexError: list index out of range

This: csv.reader(open(input_filename,'rt'),delimiter='\\t') opens the file using a tab delimiter, whereas your file is comma-delimited.

Remove the delimiter argument: csv.reader(open(input_filename,'rt')) , and the code should revert to the default comma.

Note also that any blank lines in your file will also yield an IndexError . Having a CR/LF after the last line is a common cause of confusion (it's caught me out more than once).

See csv.reader and dialects-and-formatting-parameters pages for more information.

Well, there's at least two things here.

 csv.reader(open(input_filename,'rt'),delimiter='\t')

does not correspond to your csv file, as your delimiter is clearly , and not tab .

So that line should be:

csv.reader(open(input_filename,'rt'),delimiter=',')

The second issue is that your csv file has rows enclosed by quotes, but you are not passing this information to your reader at all, which will still lead to index error, as the csv reader will interpret everything enclosed by quotes as single field. The convention is to quote fields, not rows as far as I know. One solution (though I'm sure not the most elegant) is to ignore the quotes by adding quoting=csv.QUOTE_NONE parameter and then strip the quote characters from the results before you use them:

cities = [(row[0].strip("),row[1].strip(")) for row in csv.reader(open(input_filename,'rt'),delimiter=','), quoting=csv.QUOTE_NONE]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM