尝试打开要在我的主文件中读取的外部文本文件

时间:2014-09-29 20:19:33

标签: python-3.x text-files

我的代码开始结束:

fo = open(BoulderWeatherData.csv, "r")
days=0
precip=0
lines = fo.readline()
listofpercip = []

for lines in fo:
    days = days +1
    splitting = line.split(",")
    precip = precip + float(splitting[4])
    listofpercip.append(spliting[4])
avg = round(listpercip / days, 2)

print ("the average rainfall is ", average)
print ("the max amount of rain for one day is:", max(listofpercip))

fname.close()

终端响应:

    user@cu-cs-vm:~/Dropbox$ python3 recitation4.py
Traceback (most recent call last):
  File "recitation4.py", line 5, in <module>
    fo = open(BoulderWeatherData.csv, "r")
NameError: name 'BoulderWeatherData' is not defined

我想知道文件im调用有什么问题,它被称为完全调用,并且与我的主文件在同一目录中找到....我不明白为什么它需要在所有我做的时候定义正在呼唤它......

2 个答案:

答案 0 :(得分:0)

尝试引用您的文件名,因为它是一个字符串......

fo = open("BoulderWeatherData.csv", "r")

答案 1 :(得分:0)

fo = open("BoulderWeatherData.csv","r")

我希望" "丢失,因为它是一个字符串

更多信息

Finding Maximum Value in CSV File