gpt4 book ai didi

python - VPython : File Read Error

转载 作者:太空宇宙 更新时间:2023-11-03 16:06:58 28 4
gpt4 key购买 nike

我正在使用 VIDLE 和 VPython。我想做的就是从“weather.txt”中读取值。我需要读取的值从文件的第二行开始,因此我需要跳过第一行。这是我的代码片段:

try:
filename = "‪‪‪C:\Users\Ashley\Documents\weather.txt"
except (ValueError, IndexError), e:
print e
sys.exit()

datafile = open(filename, 'r')
datafile.readline()

for line in datafile:
data = line.split()

try:
date = data[2]
temperature = float(data[3])

year = float(date[0:4])
month = float(date[4:6])
day = float(date[6:8])

decimalYear = getDecimalYear(year, month, day)

meanTimes.append(decimalYear)
meanTemperatures.append(temperature)

except:
print "Tossing line", line

datafile.close()

而且,我不断收到以下错误:

Traceback (most recent call last): File "Untitled", line 45 datafile = open(filename, 'r') IOError: [Errno 22] invalid mode ('r') or filename: '\xe2\x80\xaa\xe2\x80\xaa\xe2\x80\xaaC:\Users\Ashley\Documents\weather.txt'

你知道我到底做错了什么吗?谢谢。

最佳答案

看起来您需要转义文件路径中的反斜杠。字符串开头似乎也有不可见的 unicode 字符。

尝试使用:filename = 'C:\\Users\\Ashley\\Documents\\weather.txt'

此外,第一个 try: except: block 不是必需的,无法仅通过设置变量来引发这些异常。

关于python - VPython : File Read Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39693881/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com