1

I have uploaded a CSV file through Django and I trying to read the first line of it. The file is stored on the server in

/tmp/csv_file/test.csv

The file looks like this:

column_1,column_2,column_3
2175,294,Nuristan
2179,299,Sar-e-Pul

I am trying to get the headings of the file like:

absolute_base_file = '/tmp/csv_file/test.csv'
csv_reader = csv.reader(absolute_base_file)
csv_headings = next(csv_reader)
print csv_headings

I only get this in return:

['/']

EDITED

The permissions of the CSV file are:

-rw-rw-r--

Which should be ok.

EDITED AGAIN

Based on the recommendations and the help of @EdChum and @Moses Koledoye

I have checked if the file is read correctly using:

print (os.stat(absolute_base_file).st_size) # returns 64

Then I tried to see if seek(0) and csvfile.read(1) return a single printable character.

   print csvfile.seek(0) returns None
   print csvfile.read(1) returns 'c'

Then I thought perhaps there is a particular issue with next() function and I tried an alternative:

csv_reader = csv.reader(csvfile) 
for row in csv_reader:   
   print ("csv_reader") 

Again this didn't work.

user1919
  • 3,818
  • 17
  • 62
  • 97
  • can you post a link to csv file for me to download and try – EdChum Jun 06 '16 at 09:46
  • 1
    that loads fine for me: `['id', ' name', ' date'] first row: ['0', ' name', ' 2009-01-01']` I'm not sure what else to suggest here – EdChum Jun 06 '16 at 09:55

2 Answers2

4

You passed a string instead of a file object which is why you get the slash, change to this:

with open (absolute_base_file) as csvfile:
    csv_reader = csv.reader(csvfile)

check the docs

See this working:

In [5]:
import csv
with open (r'c:\data\csv_test.csv') as csvfile:
    csv_reader = csv.reader(csvfile)
    csv_headings = next(csv_reader)
    print (csv_headings)

['column_1', 'column_2', 'column_3']

To successively access each row call next:

In [7]:
import csv
with open (r'c:\data\csv_test.csv') as csvfile:
    csv_reader = csv.reader(csvfile)
    csv_headings = next(csv_reader)
    print (csv_headings)
    first_row = next(csv_reader)
    print( 'first row: ', first_row)


['column_1', 'column_2', 'column_3']
first row:  ['2175', '294', 'Nuristan']
EdChum
  • 376,765
  • 198
  • 813
  • 562
  • Thanks. That makes sense. But how can I access the first row of the CSV then? – user1919 Jun 06 '16 at 09:00
  • call `next` again for arbitrary row see related: http://stackoverflow.com/questions/30964244/read-specific-line-in-csv-file-python – EdChum Jun 06 '16 at 09:02
  • Hmm. I still can not make it work. Everything works till: csv_reader = csv.reader(csvfile) But when I try to access the csv_reader as: next(csv_reader) I get nothing back. – user1919 Jun 06 '16 at 09:06
  • 1
    Check if the file is being read correctly: http://stackoverflow.com/questions/2507808/python-how-to-check-file-empty-or-not – EdChum Jun 06 '16 at 09:09
  • print (os.stat(absolute_base_file).st_size) returns 64 – user1919 Jun 06 '16 at 09:14
  • I wonder if this: with open (absolute_base_file, 'rb') as csvfile: is equivalent to what you suggested: with open (r'c:\data\csv_test.csv') as csvfile: – user1919 Jun 06 '16 at 09:16
  • OK and does `seek(0)` and `csvfile.read(1)` return a single char that is printable? – EdChum Jun 06 '16 at 09:17
  • print csvfile.seek(0) returns None and print csvfile.read(1) returns 'c' – user1919 Jun 06 '16 at 09:19
  • Well if you can read it fine then the above should work, are you saying that calling `next` and printing whatever is returned doesn't work? – EdChum Jun 06 '16 at 09:27
  • Well I thought that's the case but then I tried without next() as: csv_reader = csv.reader(csvfile) for row in csv_reader: print ("csv_reader") Still no response. – user1919 Jun 06 '16 at 09:32
  • Does your csv file not contain carriage returns? i.e. it's all on a single line? Open it in a text editor to check. Also edit your question with the updated attempts and errors so the formatting isn't lost – EdChum Jun 06 '16 at 09:34
  • Hmm. This must be related. I see that when I try this: vim test.csv I get: id, name, date^M0, name, 2009-01-01^M1, another name, 2009-02-01 – user1919 Jun 06 '16 at 09:38
  • that is a carriage return `\r` can you add `'rb'` as a param to `open` – EdChum Jun 06 '16 at 09:40
  • I had 'rb' since the beginning: with open(absolute_base_file, 'rb') as csvfile: – user1919 Jun 06 '16 at 09:41
1

You should pass a file object to your csv.reader not a string literal.

absolute_base_file = open(r'/tmp/csv_file/test.csv') # notice open
csv_reader = csv.reader(absolute_base_file)
csv_headings = next(csv_reader)
print csv_headings
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • The first part of it, works but when trying to get the headings (csv_headings) then I get an error. – user1919 Jun 06 '16 at 08:59
  • I think it must not recognize the next() function. I use python 2.7. The thing is the whole code is in a try...except so it just doesn't execute it. – user1919 Jun 06 '16 at 09:01
  • problem with your path? – Moses Koledoye Jun 06 '16 at 09:09
  • I think no. This is what I get back when I print (csv_reader): <_csv.reader object at 0x2f466e0> I think this implies its accessible. – user1919 Jun 06 '16 at 09:12
  • 1
    Try `csv_reader.next()` as shown in the [docs](https://docs.python.org/2/library/csv.html#csv.csvreader.next). Not sure if that changes anything though. – Moses Koledoye Jun 06 '16 at 09:18
  • Nope. Same thing. I wonder if this is related with permissions. Shouldn't be Read permissions enough? – user1919 Jun 06 '16 at 09:23