How to read csv files correctly using pandas?

Viewed 60

I'm having a csv file like below. I need to check whether the number of columns are greater than the max length of rows. Ex,

name,age,profession
"a","24","teacher","cake"
"b",31,"Doctor",""
"c",27,"Engineer","tea"

If i try to read it using

print(pd.read_csv('test.csv'))

it will print as below.

      name       age profession
  a    24   teacher       cake
  b    31    Doctor        NaN
  c    27  Engineer        tea

But it's wrong. It happened due to the less number of columns. So i need to identify this scenario as a wrong csv format. what is the best way to test this other than reading this as string and testing the length of each row.

And important thing is, the columns can be different. There are no any mandatory columns to present.

1 Answers

You can try put header=None into .read_csv. Then pandas will throw ParserError if number of columns won't match length of rows. For example:

try:
    df = pd.read_csv("your_file.csv", header=None)
except pd.errors.ParserError:
    print("File Invalid")
Related