Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中循环处理文本文件_Python_Loops - Fatal编程技术网

如何在python中循环处理文本文件

如何在python中循环处理文本文件,python,loops,Python,Loops,我需要有关文件循环的帮助: 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

我需要有关文件循环的帮助:

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[]
等等

import glob
g=glob.glob("*") #This selects every file in directory, if you want the only the ones starting with "temps", then write "temps*"
for index, line in enumerate(g):
    print(g)
    #do stuff with files here
g是您的文件列表。最后一个for循环就是在它们上面的循环。这有用吗

评论后的修改 这对我很有用:

import glob
g=[]
for x in range(inputdata):
    select = "*"+str(x)
    exp_g=glob.glob(select)
    g.extend(exp_g)
g

创建列表列表,而不是单独的列表。这有助于更好地管理事情。然后遍历文件,将内容放入先前创建的列表的每个子列表中

inputa = int(input('How many days of data do you have? '))
print('Average Temperatures:')

lst = [[] for _ in range(inputa)]

for i in range(inputa):
    for line in open(f'temps{i+1}.txt'):
        lst[i].append(line.rstrip())

print(lst)  # List of lists.
print([item for sublist in lst for item in sublist])  # Flattened list.

类似于
files=['temps1.txt','temps2.txt','temps3.txt']
然后
为范围内的i(int(inputa)):
然后逐个打开文件one@Bazingaa我该怎么说呢?你已经得到了下面两个答案来解决这个问题。我想要的文件取决于我的输入。如果我的用户输入2,那么它应该选择两个文件,即temp1和temp2。你看到我代码中的模式了吗?如果我想合并列表呢。您可以这样编程输出:['a:2','b:3']['a:7','b:3']。然而,我希望它是['a:2','b:3','a:7','b:3']。