我有一个包含一些值的.txt文件。我需要使用python一次获取两个值并对其执行一些操作,然后获取接下来的两个值。
我想写一个python脚本,我可以一次获取两个值。我是python的新手。
谢谢!
答案 0 :(得分:0)
# Read the file first and split lines in a list
with open('temp.txt', 'r') as f:
lines = f.read().splitlines()
# Loop through lines, two at a time
for i in range(0, len(lines), 2): # Notice the last argument '2', it makes the loop step 2 at a time
val1 = lines[i]
val2 = lines[i+1]
# Perform calculation on val1 and val2
希望这有帮助。