Python 带有嵌入引号的文件名的“CSV 文件不存在”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39267614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:03:30  来源:igfitidea点击:

"CSV file does not exist" for a filename with embedded quotes

pythoncsvpandasatom-editor

提问by Aleksei Nabatov

I am currently learning Pandas for data analysis and having some issues reading a csv file in Atom editor.

我目前正在学习 Pandas 进行数据分析,并且在 Atom 编辑器中读取 csv 文件时遇到了一些问题。

When I am running the following code:

当我运行以下代码时:

import pandas as pd 

df = pd.read_csv("FBI-CRIME11.csv")

print(df.head())

I get an error message, which ends with

我收到一条错误消息,以

OSError: File b'FBI-CRIME11.csv' does not exist

OSError:文件 b'FBI-CRIME11.csv' 不存在

Here is the directory to the file: /Users/alekseinabatov/Documents/Python/"FBI-CRIME11.csv".

这是文件的目录:/Users/alekseinabatov/Documents/Python/"FBI-CRIME11.csv"。

When i try to run it this way:

当我尝试以这种方式运行它时:

df = pd.read_csv(Users/alekseinabatov/Documents/Python/"FBI-CRIME11.csv")

I get another error:

我收到另一个错误:

NameError: name 'Users' is not defined

NameError:未定义名称“用户”

I have also put this directory into the "Project Home" field in the editor settings, though I am not quite sure if it makes any difference.

我也将此目录放入编辑器设置中的“Project Home”字段中,但我不太确定它是否有任何区别。

I bet there is an easy way to get it to work. I would really appreciate your help!

我敢打赌,有一种简单的方法可以让它发挥作用。我将衷心感谢您的帮助!

回答by arutaku

Have you tried?

你有没有尝试过?

df = pd.read_csv("Users/alekseinabatov/Documents/Python/FBI-CRIME11.csv")

or maybe

或者可能

df = pd.read_csv('Users/alekseinabatov/Documents/Python/"FBI-CRIME11.csv"')

(If the file name has quotes)

(如果文件名有引号)

回答by BartDur

Just referring to the filename like

只是指像这样的文件名

df = pd.read_csv("FBI-CRIME11.csv")

generally only works if the file is in the same directory as the script.

通常仅当文件与脚本位于同一目录中时才有效。

If you are using windows, make sure you specify the path to the file as follows:

如果您使用的是 Windows,请确保按如下方式指定文件路径:

PATH = "C:\Users\path\to\file.csv"

回答by Adnane

Had an issue with the path, it turns out that you need to specify the first '/' to get it to work! I am using VSCode/Python on macOS

路径有问题,事实证明您需要指定第一个“/”才能使其工作!我在 macOS 上使用 VSCode/Python

回答by ?rem ?ahin

I also experienced the same problem I solved as follows:

我也遇到了我解决的相同问题,如下所示:

dataset = pd.read_csv('C:\Users\path\to\file.csv')

回答by user3403899

Being on jupyter notebookit works for me including the relative path only. For example:

jupyter notebook 上它只对我有用,包括相对路径。例如:

df = pd.read_csv ('file.csv')

But, for example, in vscodeI have to put the complete path:

但是,例如,在vscode 中,我必须输入完整路径:

df = pd.read_csv ('/home/code/file.csv')

回答by broken_arrow

You are missing '/' before Users. I assume that you are using a MAC guessing from the file path names. You root directory is '/'.

您在用户之前缺少“/”。我假设您使用的是从文件路径名中猜测的 MAC。您的根目录是“/”。

回答by Ramkumar Arunachalam

Just change the CSV file name. Once I changed it for me, it worked fine. Previously I gave data.csvthen I changed it to CNC_1.csv.

只需更改 CSV 文件名。一旦我为我改变它,它工作得很好。以前我给了data.csv然后我把它改成了CNC_1.csv.

回答by DragoRoff

Run "pwd" command first in cli to find out what is your current project's direction and then add the name of the file to your path!

首先在 cli 中运行“pwd”命令以找出您当前项目的方向,然后将文件名添加到您的路径中!

回答by Brett Young

Try this

尝试这个

import os 
cd = os.getcwd()
dataset_train = pd.read_csv(cd+"/Google_Stock_Price_Train.csv")

回答by Chris Marsh

What worked for me:

什么对我有用:

import csv
import pandas as pd
import os

base =os.path.normpath(r"path")



with open(base, 'r') as csvfile:
    readCSV = csv.reader(csvfile, delimiter='|')
    data=[]
    for row in readCSV:
        data.append(row)
    df = pd.DataFrame(data[1:],columns=data[0][0:15])
    print(df)


This reads in the file , delimit by |, and appends to list which is converted to a pandas df (taking 15 columns)