为什么我在读取csv文件时出错?

2024-04-28 22:10:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着打开一个csv文件,这样我就可以将我的彩票号码(从1到54随机生成的6个整数)与1992年至2017年的中奖号码进行比较,这也是csv文件所包含的内容。在

import random

import csv

six_random_int =[random.randint(1,54),random.randint(1,54),random.randint(1,54),random.randint(1,54),random.randint(1,54),random.randint(1,54)]

print('Your lotto ticket is ',six_random_int)

with open('lottotexas.csv','r') as f:  #This is where the error is occurring
    reader = csv.reader(f)
    for row in reader:
        print(row)

我一直收到错误:

^{pr2}$

我把csv文件下载到我的电脑上,但我不知道从那里到哪里,这样我的python程序就可以找到csv文件。在


Tags: 文件csvimportis整数randomreader号码
3条回答

请将文件复制并粘贴到python程序所在的同一路径(folder/directory/place)。 再次运行你的程序。 如果问题仍然存在,请检查文件权限。在

请尝试指定文件的完整路径。必须让python在文件所在的同一文件夹中执行。在

error中可以很清楚地看到:

with open('lottotexas.csv','r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'lottotexas.csv'. 

Python找不到文件'lottotexas.csv'。在

Python尝试open文件的方法是在.pyfile的当前directory中查找{},除非您已经给出了一个完整的file path。如果给file一个完整的path,那么Python将从该位置打开{}。在

尽管如此,按照您编写这个程序的方式,Python试图在与Python file相同的file中找到一个名为:lottotexas.csvfile,而且没有{}来命名它!-因此errorFileNotFoundError!在

相关问题 更多 >