文本文件如下所示:
4-1
9-3新的文本文件在定稿时应该这样看:
4-1=3
9-3=6我被困在一开始
handle = open('izrazi.txt', 'r')
read = handle.readlines()
# getting the list here and I don't know how to calculate and my result 发布于 2020-11-28 18:06:34
首先,您需要使用以下语句定义并打开要写入结果的输出文件:
with open然后从输入(for line in read)迭代您的行,使用eval从字符串(eval(line))计算操作,并将其写入指定的目标文件,并将读取的行和获得的结果(output.write(line + "=" + str(res)+"\n"))连接起来。
handle = open('izrazi.txt', 'r')
read = handle.readlines()
with open("output.txt", "w") as output:
for line in read:
res = eval(line.strip())
output.write(line.strip()+"="+str(res)+"\n")https://stackoverflow.com/questions/65052874
复制相似问题