tags:

views:

85

answers:

3

I am using csv.reader to read file but it giving me whole file.

file_data = self.request.get('file_in');
file_Reader = csv.reader( file_data );
for fields in file_Reader:

I want one line at a time and separate data from that line.

ex: filedata = name,sal,dept
               x,12000,it
o\p=
name
sal
dept
.
.
.
+1  A: 

This

>>> import csv
>>> spamReader = csv.reader(open('eggs.csv'), delimiter=' ', quotechar='|')
>>> for row in spamReader:
...     print ', '.join(row)
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam

Was taken from the manual

Hope that helps...

Martin Milan
clue =- Row is a list of strings there...
Martin Milan
+1 even if for nothing else but the Monty Python reference.
Chen Levy
it is giving me whole file in a single line
Rahul99
@rahul: show us a few lines of the file!!!
John Machin
My point is that by the time the code reaches the print line, row should be a list of the items in a specific row of your csv. Is this not what you're seeing?
Martin Milan
A: 

Why not do it manually?

for line in fd:
        foo, bar, baz = line.split(";")

Greets, Philip

Philip
That wouldn't handle things like quotes correctly though, would it - which would be needed if the data in the csv could, in itself, contain commas...
Martin Milan
+1 for the hint. This is a common speed-vs-comfort trade-off.
Philip
A: 

It looks like you are trying to pass a string of data directly to csv.reader(). It's expecting an iterable object like a list or filehandle. The docs for the csv module mention this. So you probably want to split the string along newlines before passing it to csv.reader.

import csv
file_data = self.request.get('file_in')
file_data_list = file_data.split('\n')
file_Reader = csv.reader(file_data_list)
for fields in file_Reader:
    print row

Hope that helps.

razzmataz
yes, it works.thanks for help.
Rahul99