2017-08-17 85 views
2

我在(C:/Users/kgu/PycharmProjects/untitled1/test1.py)中保存了csv文件。命名为testdata.csv在Python中打开csv文件时出错

我的代码是:

import csv 
f=open('testdata.csv','r',encoding='utf-8') 

和错误消息是在这里:

C:\Users\kgu\Anaconda3\python.exe 
C:/Users/kgu/PycharmProjects/untitled1/test1.py 
Traceback (most recent call last): 
File "C:/Users/kgu/PycharmProjects/untitled1/test1.py", line 3, in <module> 
f=open('testdata.csv','r',encoding='utf-8') 
FileNotFoundError: [Errno 2] No such file or directory: 'testdata.csv' 

Process finished with exit code 1 

我没有保存testdata.csv在Python项目文件夹。 我不知道它有什么问题。

+0

你从哪里跑了吗? (工作目录),这个目录里有什么?文件的完整路径是什么? – Hagai

+0

您应该检查您的csv文件是否与您的python脚本位于相同的目录中。 –

+0

将您的csv放在C:/ Users/kgu/PycharmProjects/untitled1/ –

回答

0

如果使用熊猫,你可以试试:

import pandas as pd 

filepath = "/yourfilepath/yourfile.csv" 
# check your sep (";" , "," ...) 
f = pd.read_csv(filepath, encoding="utf-8", sep="yoursep") 
0

你可以做这样的 - 如果你是在正确的工作目录:

with open(testdata.csv', 'rb') as csvfile: 
    data = [x for x in csv.reader(csvfile, delimiter='yourdelimiter')]