我需要一个循环文件的帮助:
inputa = int(input('How many days of data do you have? '))
print('Average Temperatures:')
if inputa == 1:
lst1 = []
lst = []
for line in open('temps1.txt'):
lst1.append(line.rstrip())
lst = lst1
elif inputa == 2:
lst1 = []
lst2 = []
lst = []
for line in open('temps1.txt'):
lst1.append(line.rstrip())
for line in open('temps2.txt'):
lst2.append(line.rstrip())
lst = lst1+lst2
elif inputa == 3:
lst1 = []
lst2 = []
lst3 = []
lst = []
for line in open('temps1.txt'):
lst1.append(line.rstrip())
for line in open('temps2.txt'):
lst2.append(line.rstrip())
for line in open('temps3.txt'):
lst3.append(line.rstrip())
lst = lst1 + lst2 + lst3
有没有办法循环文件。例如,根据用户的输入,我希望它们是temps1.txt
、temps2.txt
、temps3.txt
等等。我还想要lst1[]
,lst2[]
,lst3[]
等等。你知道吗
g是你的文件列表。最后一个for循环就是在它们上面循环。这有用吗?你知道吗
评论后修改
这对我很有用:
创建列表列表,而不是单独的列表。这有助于更好地管理事情。然后遍历文件并将内容放入先前创建的列表的每个子列表中。你知道吗
相关问题 更多 >
编程相关推荐