无法从C盘读取CSV文件[已解决]

时间:2019-06-08 12:33:39

标签: python pandas

尝试在Pycharm中使用pandas的python文件读取CSV文件。运行代码时出现错误 我尝试了所有过去的解决方案,例如添加“ r”或双斜杠或提及编码。它似乎不起作用

使用熊猫库中的read_csv 尝试在文件路径中使用双斜杠,但没有成功

更新 更新了如下代码 显然,CSV文件的问题之一是文件中没有标题。下面工作得很好

将熊猫作为pd导入 路径=“ 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”]) 打印(df)

import pandas as pd

df = pd.read_csv("C:\ML_Cricket\CSV\221571.csv")

print(df.head())

我得到此追溯:

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

1 个答案:

答案 0 :(得分:0)

也许尝试一下。

df = pd.read_csv("c:/ML_Cricket/CSV/221571.csv")