【问题标题】:Trying to open an external text file to be read in my main file尝试打开要在我的主文件中读取的外部文本文件
【发布时间】:2014-09-29 20:19:33
【问题描述】:

我的代码从头到尾:

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

我想知道我调用的文件有什么问题,它被准确地调用并且与我的主文件在同一目录中找到....我不明白为什么在我做的所有事情时都需要定义它正在调用它....

【问题讨论】:

  • 我自己想通了,我犯了一个简单的错误...没有“”我的文件名.....哇

标签: python-3.x text-files


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    fo = open("BoulderWeatherData.csv","r")
    

    我希望" " 不见了,因为它是一个字符串

    更多信息

    Finding Maximum Value in CSV File

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      相关资源
      最近更新 更多