简体   繁体   English

Python错误:CSV文件错误,读取/打印CSV文件

[英]Python Error:CSV File Error, Read/Print CSV file

I am getting this error when trying to print the contents of a CSV file in Python. 尝试在Python中打印CSV文件的内容时出现此错误。

Traceback (most recent call last): File "/Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/Player.py", line 5, in with open('player.csv') as csvfile: FileNotFoundError: [Errno 2] No such file or directory: 'player.csv' 追溯(最近一次通话):文件“ /Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/Player.py”,第5行,以open('player.csv')作为csvfile:FileNotFoundError:[Errno 2]没有这样的文件或目录:'player.csv'

Get the exact file path to the csv, if you are on a windows get the entire folder path and then the name, and then do: 获取csv的确切文件路径,如果您在Windows上,则获取整个文件夹路径,然后获取名称,然后执行以下操作:

with open(r'C:\users\path\players.csv') as csvfile: 

If you're using a windows and the exact path, easiest to put the r before the path like I did because it is a literal which will allow the string to be interpreted and the path to be found. 如果您使用的是Windows和确切的路径,则最容易像我一样将r放在路径之前,因为它是文字,可以解释字符串并找到路径。

You must put player.csv to the same location with your script Player.py 您必须将player.csv与脚本Player.py放在同一位置

Example like your code, both files should be here: /Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/ 像您的代码这样的示例,两个文件都应该在这里: /Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/

Or you can put the specific directory of player.csv , 或者,您可以放置player.csv的特定目录,

Ex: 例如:

with open("/Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/player.csv") as csvfile:
...

Check that you have the file in question in same directory as your .py file you're working on. 检查您要处理的文件与正在处理的.py文件位于同一目录中。 If that doesn't work you might want to use the full path to the file: 如果这样不起作用,则可能要使用文件的完整路径:

with open ('/Users/cassandracampbell/Library/Preferences/PyCharmCE2018.2/scratches/player.CSV') as csvfile:

And you should try to check the name of the file too should be case sensitive otherwise, let's say you have Player.csv then don't try to open player.csv all lower case won't work! 并且您还应该尝试检查文件名,也应该区分大小写,否则,假设您有Player.csv,那么不要尝试打开player.csv,所有小写字母都将不起作用!

Plus I don't know what you're trying to do with your CSV file, just print the raw content? 另外,我不知道您要使用CSV文件做什么,仅打印原始内容? You might like using pandas. 您可能会喜欢使用熊猫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM