Trying to read CSV file using pandas in Pycharm for a python project. Getting an error when i run the code I tried all the past solutions like adding "r" or double slash or mentioning the encoding. It does not seem to work.
Using read_csv
using pandas library.
Tried using double slash in the file path but it didn't work.
UPDATE updated the code as below Apparently, one of the issues with the CSV file was that there was no header in the file. Below worked just fine:
import pandas as pd
path = "c:/ML_Cricket/CSV/225171.csv"
df = pd.read_csv(path,error_bad_lines=False,names=["1","2","3","4","5","6","7","8","9","10","11"])
print(df)
import pandas as pd
df = pd.read_csv("C:\ML_Cricket\CSV\221571.csv")
print(df.head())
I get this traceback:
C:\Users\abc\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/abc/.PyCharmCE2019.1/config/scratches/scratch.py
Traceback (most recent call last):
File "C:/Users/abc/.PyCharmCE2019.1/config/scratches/scratch.py", line 3, in <module>
df = pd.read_csv("C:\ML_Cricket\CSV\221571.csv")
File "C:\Users\abc\PycharmProjects\untitled\venv\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\abc\PycharmProjects\untitled\venv\lib\site-packages\pandas\io\parsers.py", line 429, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\abc\PycharmProjects\untitled\venv\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
self._make_engine(self.engine)
File "C:\Users\abc\PycharmProjects\untitled\venv\lib\site-packages\pandas\io\parsers.py", line 1122, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:\Users\abc\PycharmProjects\untitled\venv\lib\site-packages\pandas\io\parsers.py", line 1853, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libs\parsers.pyx", line 387, in pandas._libs.parsers.TextReader.__cinit__
File "pandas\_libs\parsers.pyx", line 686, in pandas._libs.parsers.TextReader._setup_parser_source
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character