pandas 无法打开CSV文件FileNotFoundError [英] Pandas can't open a csv file FileNotFoundError

查看:54
本文介绍了 pandas 无法打开CSV文件FileNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难用 Pandas 打开csv.我已经在《为每个人的熊猫》(Pandas for Everyone)书中尝试过该示例,已经搜索了很多遍,最后一个示例在这里 https://analytics4all.org/2016/05/09/python-read-csv-and-excel-with-pandas/python程序位于名为"lbcsv"的文件夹中,然后,csv文件位于该文件夹中的另一个文件夹中,称为csv.我尝试不使用完整路径,将csv文件放置在与程序相同的文件夹中,然后将其移动到同一目录中的另一个文件夹中.只是似乎不想打开csv.我尝试了没有编码和Sep.在此之前,我必须先卸载 pandas numpy ,然后重新安装,因为它给出有关 numpy 的错误.我之前已经解决了这个问题,但是没有在这里问这个问题,所以我没有关于如何执行此操作的文档.

I am having a hard time opening a csv with Pandas. I have tried the example in 'Pandas for Everyone' book, have googled many times and last example was here https://analytics4all.org/2016/05/09/python-read-csv-and-excel-with-pandas/ The python program is in a folder called 'lbcsv' then the csv files are in another folder within that one called csv. I have tried not using full path, placing csv file in same folder as the program, then moving them to another folder in the same directory. Just does not seem to want to open the csv. I have tried without the encoding and sep. Before this I had to uninstall pandas and numpy then reinstall because it was giving an error about numpy. I have got around this before but did not ask the question on here so I have no documentation of how I did so.

import pandas as pd
import numpy

servers = pd.read_csv('C:\\Users\\a089673\\Desktop\\lbcsv\\csv\\server.csv', encoding='utf-8', sep=',')
print(servers.head())

Traceback (most recent call last):
  File "C:/Users/a089673/Desktop/lbcsv/pandaslb.py", line 4, in <module>
    servers = pd.read_csv('C:\\Users\\a089673\\Desktop\\lbcsv\\csv\\server.csv', encoding='utf-8', sep=',')
FileNotFoundError: File b'C:\\Users\\a089673\\Desktop\\lbcsv\\csv\\server.csv' does not exist

推荐答案

出于完整性考虑, \ a 是转义字符,这会给您带来麻烦.使用注释中提到的原始字符串可以解决此问题.

For completeness, \a is an escape character and that is causing issue for you. Using raw string as mentioned in the comments solve this issue.

如果执行 repr(file_path),则可以清楚地看到这一点.这是Windows的陷阱之一.我建议即使在Windows中也使用正斜杠来访问文件,以避免遇到这些问题.

You can see this clearly if you do repr(file_path) . This is one of the gotchas with Windows. I would suggest using forward slashes for accessing files even in Windows to avoid running into these issues.

这篇关于 pandas 无法打开CSV文件FileNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆