Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.2 skip a line in csv.DictReader

Tags:

How do I skip a line of records in a CSV when using a DictReader?

Code:

import csv reader = csv.DictReader(open('test2.csv')) # Skip first line reader.next() for row in reader:     print(row) 

Error:

Traceback (most recent call last):   File "learn.py", line 3, in <module>     reader.next() AttributeError: 'DictReader' object has no attribute 'next' 
like image 645
paragbaxi Avatar asked Jan 24 '11 22:01

paragbaxi


People also ask

How do I skip a line in CSV?

In Python, while reading a CSV using the CSV module you can skip the first line using next() method.

How do I skip a blank line in Python CSV?

If you want to skip all whitespace lines, you should use this test: ' '. isspace() . Beware: This method is likely to clobber files having newlines inside quoted fields. In this case the number of lines in the file is not comparable to the number of delimited records.

How do I skip the first row in a CSV file in Python?

Line 1: We import the Pandas library as a pd. Line 2: We read the csv file using the pandas read_csv module, and in that, we mentioned the skiprows=[0], which means skip the first line while reading the csv file data. Line 4: Now, we print the final dataframe result shown in the above output without the header row.


1 Answers

You use next(reader) instead.

Source: csv.DictReader documentation

like image 172
Sebastian Paaske Tørholm Avatar answered Sep 27 '22 23:09

Sebastian Paaske Tørholm