从.txt文件获取python中的两个值
我有一个包含一些值的.txt文件。我需要使用python一次获取两个值,并对其执行一些操作,然后获取接下来的两个值 我想编写一个python脚本,在这里我可以一次获取两个值。我是python新手 谢谢从.txt文件获取python中的两个值,python,Python,我有一个包含一些值的.txt文件。我需要使用python一次获取两个值,并对其执行一些操作,然后获取接下来的两个值 我想编写一个python脚本,在这里我可以一次获取两个值。我是python新手 谢谢 # 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 ti
# 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
希望这有帮助。文件的格式是什么?例如,是否有一行有多个值,多行每行有2个值,多行每行有一个值,等等。请提供更多信息,说明您迄今为止尝试了什么,txt文件有多少个条目,以及您以后是否需要这些值,还是只需要一次计算。txt文件有一个值(浮点数)每行。