【问题标题】:How to properly format a filepath on Windows when using Pandas? [closed]使用 Pandas 时如何在 Windows 上正确格式化文件路径? [关闭]
【发布时间】:2020-06-26 14:37:29
【问题描述】:

我是 Pandas 的初学者,尝试使用 Pandas 读取此 CSV 文件时,找不到该文件。

>>> df = pd.read_csv('D:\Python\datasets\tweets')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\amr.bibars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\io\parsers.py", line 676, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\amr.bibars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\io\parsers.py", line 448, in _read
    parser = TextFileReader(fp_or_buf, **kwds)
  File "C:\Users\amr.bibars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\io\parsers.py", line 880, in __init__
    self._make_engine(self.engine)
  File "C:\Users\amr.bibars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\io\parsers.py", line 1114, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "C:\Users\amr.bibars\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas\io\parsers.py", line 1891, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 374, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas\_libs\parsers.pyx", line 674, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] File D:\Python\datasets    weets does not exist: 'D:\\Python\\datasets\tweets'
>>> pd.read_csv('D:\Python\datasets\cars.csv')
  Unnamed: 0  cars_per_cap        country  drives_right
0         US           809  United States          True
1        AUS           731      Australia         False
2        JAP           588          Japan         False
3         IN            18          India         False
4         RU           200         Russia          True
5        MOR            70        Morocco          True
6         EG            45          Egypt          True

enter image description here

【问题讨论】:

  • 检查该路径中是否存在文件。
  • 您的错误表明该文件不存在。这可能意味着该目录中的文件确实丢失了,或者您提供的文件路径格式不正确。请注意 Windows 反斜杠后跟 r, n, c, b, t 成为转义字符。在复制粘贴的情况下,也可能是拼写错误,甚至是“隐形字符”。有关 Windows 中转义字符的更多详细信息,请参阅此答案:stackoverflow.com/a/6928938/4847576

标签: python pandas dataframe


【解决方案1】:

如果您确定该文件存在于该目录中,则可能是因为您使用了反斜杠,因此 \tweets.csv 中的 \t 已被解释为制表符。

在 Windows 中,在提供带有反斜杠的 Python 路径时需要注意。

正如我在评论中指出的那样,r, n, c, b, t 和其他以反斜杠开头的将被解释为转义字符。

Check the table in this section of the Python docs 获取完整列表。

解决此问题的一种方法是通过在路径字符串前面加上 r 来使用原始字符串:

path = r'D:\Python\datasets\tweets.csv'

【讨论】:

  • 感谢您的帮助,我确实从文件名中删除了 t 并重命名,但仍然收到相同的错误
  • @Baibars 无赖,您尝试过使用原始字符串吗?
  • 我使用了路径函数但不起作用,但现在我只在字符串之前使用了 r 并且它起作用了! :) pandas.read_csv(r"文件路径")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多