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'
In Python, while reading a CSV using the CSV module you can skip the first line using next() method.
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.
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.
You use next(reader)
instead.
Source: csv.DictReader documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With