我输入了一个程序来比较车辆 A 价格 (v_priceA) 与 carprices.txt 文本文件中的其他各种车辆价格,这些车辆价格位于新行中。
结果应该是一个名为 highprices.txt 的新文本文件,其中所有价格都高于车辆 A 的价格,并在换行符中加上 carprices.txt 中的相关行号
我的问题是能够生成两个文本文件,它们具有更大文件的行号,另一个具有更大的价格,而不是更大的价格本身和行号。我需要解决这个问题。
车辆A价格:2500.50
v_priceA = 2500.50
a_file = 'carprices.txt'
with open(a_file, 'r') as document:
values = [x for x, value in enumerate(document) if float(value) > v_priceA]
new_file = open('highpriceposition.txt', 'w')
for x in values:
new_file.write(str(x) + '\n')
new_file.close()
a_file = 'carprices.txt'
with open(a_file, 'r') as document:
values = [value for value in document if float(value) > v_priceA]
with open('highprice.txt', 'w') as f:
for x in values:
f.write(str(x)+'\n')
位置价格.txt
2 2900.00
3 3500.50
5 25000.30
6 45000.50
慕少森
相关分类