PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 27, 2022

[FIXED] How to remove the pre header line item of a CSV file using csv.DictReader

 August 27, 2022     csv, dictionary, python-3.x, reader, utf-8     No comments   

Issue

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.


Solution

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)


Answered By - blhsing
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing