我试图从txt中获取一些值,增加它们(直到这里一切都好)然后在文件上写新变量但是我不能在文件上写变量虽然我已经改变了变量在一个字符串.. 代码是
with open("setup.txt", "r") as f:
for i, line in enumerate(f):
str = line.split(",")
if i == 0:
minL = int(str[0])
maxL = int(str[1])
minL += 2
maxL += 2
elif i == 1:
minF = int(str[0])
maxF = int(str[1])
minF += 1
maxF += 1
minL = str(minL)
with open("setup.txt", "w") as f:
f.write(minL)
f.close()
txt只是:
15, 25
2, 9
EDIT ********* 对不起我复制代码时犯了一个错误,我已经把“w”写入了写入模式,但这不起作用
错误是
line 15, in <module>
minL = str(minL)
NameError: name 'minL' is not defined
但我定义了minL
答案 0 :(得分:0)
17
是您的预期输出吗?
我复制并修改了你的代码:
with open("setup.txt", "r") as f:
for i, line in enumerate(f):
stri = line.split(",")
if i == 0:
minL = int(stri[0])
maxL = int(stri[1])
minL += 2
maxL += 2
elif i == 1:
minF = int(stri[0])
maxF = int(stri[1])
minF += 1
maxF += 1
minL = str(minL)
with open("setup.txt", "w") as f:
f.write(minL)
f.close()
我的缩进错误(我已修复)并将str = line.split(",")
更改为stri = line.split(",")