Read a csv file in python correctly using pandas

Question:

I am trying to read this file using read_csv in pandas(python).
But I am not able to capture all columns.
Can you help?

Here is the code:

file = r'path of file'
df = pd.read_csv(file, encoding='cp1252', on_bad_lines='skip')
Asked By: rish

||

Answers:

I tried to read your file, and I first noticed that the encoding you specified does not correspond to the one used in your file. I also noticed that the separator is not a comma (,) but a tab (t).

First, to get the file encoding (in linux), you just need to run:

$ file -i kopie.csv 
kopie.csv: text/plain; charset=utf-16le

In Python:

import pandas as pd

path_to_file = 'kopie.csv'
df = pd.read_csv(path_to_file, encoding='utf-16le', sep='t')

And when I print the shape of the loaded dataframe:

>>> df.shape
(869, 161)
Answered By: inarighas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.