Home > Software engineering >  How to remove the pre header line item of a CSV file using csv.DictReader
How to remove the pre header line item of a CSV file using csv.DictReader

Time:08-16

I would like to remove the first line from a csv file like below which comes with the encode type, so when I`m reading that file with csv.DictReader it is recognizing this word as a key from the dictionary. csv input: (raw_file_data)

UTF-8,,,,
POSID,POST1,VERNR,PBUKR,PWPOS
"B00007","testing",08027011,"0030","CNY"

code to read it:

for row in csv.DictReader(codecs.getreader(encoding="ISO-8859-1")(raw_file_data)):
    data_list.append(row)

the result is that the first line of CSV is being considered as a key and that`s my issue, can anyone help me to just ignore that first line and consider the csv reader since the second line which contains the header information?, I tried something with next(), but I could not solve it.

Many thanks.

CodePudding user response:

You can skip the first line by calling next on the file iterator before you pass it to csv.DictReader:

file = codecs.getreader(encoding="ISO-8859-1")(raw_file_data)
next(file)
for row in csv.DictReader(file):
    data_list.append(row)
  • Related