Edit: Per your updated response, it looks like you are getting the actual file contents in csv_file
, and your newline separator is \r\n
. Therefore I would suggest trying the following:
In [1]: import csv
In [2]: csv_file = # Copied the response from your error into this variable
In [3]: reader = csv.reader(csv_file.split('\r\n'))
In [4]: for row in reader:
...: if row:
...: print row[0]
...:
...:
name
Amazon
B Shop
Otto
The if row
check is done because you can see that the end of the CSV string is \r\n
, which means that when you split on the \r\n
character, the last item of the list will be empty. When that empty element is read by csv.reader
, the result is an empty list, which as you know will fail with an IndexError
if you try to do any sort of accessing by index. Alternatively, you could do this beforehand to ensure that all 'rows' contain values:
In [8]: csv_file = [row for row in csv_file.split('\r\n') if row]
In [9]: reader = csv.reader(csv_file)
In [10]: for row in reader:
....: print row[0]
....:
....:
name
Amazon
B Shop
Otto
I'm not sure of your use case, but if you are using your code to upload data to your app, I would suggest taking a look at the Remote API - this provides a very clean way to go about doing that and precludes you from having to use special handlers, etc.
I'll make the assumption that you are passing in an actual CSV (not a string name of a file - if you are passing in a filename see below), so something like this:
some_csv = 'header1,header2\nvalue1,value2'
What you're doing is splitting on the newlines and passing csv.reader
an object over which to iterate, the result of which is a csv.reader
object. Note that you can iterate directly over this object, so you do not need to create a separate dataset
variable:
In [1]: import csv
In [2]: some_csv = 'header1,header2\nvalue1,value2'
In [3]: fileReader = csv.reader(some_csv.split("\n"))
In [4]: fileReader
Out[4]: <_csv.reader object at 0x1306440>
In [5]: for data in fileReader:
...: print data
...:
...:
['header1', 'header2']
['value1', 'value2']
Using print data[1]
will print the second item of each list (as mentioned by @sadaf2605, Python uses zero-based indexing, so the first element would actually be data[0]
). Your error indicates that there is only one element per row, so the first thing to check would be whether or not you are actually passing comma-separated values into that parameter.
However you also mention that you are importing a CSV file - is your URL parameter a string reference to a filename itself? If so, that will cause an issue because you won't actually be opening the file - you will be iterating over an object that only contains the name of the file itself, not the data:
In [1]: import csv
In [2]: some_csv = 'some_csv.csv'
In [3]: fileReader = csv.reader(some_csv.split("\n"))
In [4]: for data in fileReader:
...: print data
...:
...:
['some_csv.csv']
Again on the zero-based indexing, you can see that there is not a second element to this array, so you will get the IndexError
. If this is your case, you can try this (although I would again suggest the Remote API :) )
In [1]: import csv
In [2]: some_csv = 'some_csv.csv'
In [3]: with open(some_csv, 'rb') as f:
...: fileReader = csv.reader(f)
...: for data in fileReader:
...: print data[0]
...:
...:
header1
value1