Reading csv file

Igor Korot ikorot01 at gmail.com
Tue Dec 17 10:53:35 CET 2013


Hi, guys,

On Tue, Dec 17, 2013 at 12:55 AM, Peter Otten <__peter__ at web.de> wrote:
> Peter Otten wrote:
>
>> You are still reading the complete csv file. Assuming
>>
>> (1) the first row of the csv contains the column names
>> (2) you want to skip the first five rows of data

Looking at the Peter's reply I realized I missed very important piece:

The first row may or may not contain column names.
If it does not, the first row will just contain some text, i.e. "abc"
and the column names will be located on the row 6.

I know if does complicate things but I am deeply sorry.
The csv file is generated by some program run and I guess depending on
the switches passed to
that program it either creates the header in the csv (report name,
time slice it ran at, user it ran under
and some other info.
Or it can be run without such switch and then it generates a normal csv.

The report it generates is huge: it has about 30+ fields and I need to
read this report, parse it and
push accordingly to the database of mySQL.

Thank you for any suggestions and sorry for not posting complete task.

>>
>> you'd have to write
>>
>> reader = csv.Reader(file)
>
> Sorry, I meant DictReader, not Reader.
>
>> line = 0
>> while line < 5:
>>     next(reader)
>>     line += 1
>> for row in reader:
>>     .... # process csv row
>>
>> A simpler alternative is to use itertools.islice():
>>
>> for row in itertools.islice(reader, 5, None):
>>     ... # process csv row
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list