我有一个文件上传页面,用户上传他们的文件,他们通常都是files.In --我的python代码--我试图从该文件中提取一个标记,然后保存到一个列表中,所以一切都很好,但是这里我得到了3个文件的三个不同的输出列表。如何将3个输出列表组合成one.Here是我的代码
a=self.filename
print(a) #this prints out the uploaded file names(ex: a.xml,b.xml,c.xml)
soc_list=[]
for soc_id in self.tree.iter(tag='SOC_ID'):
req_soc_id = soc_id.text
soc_list.append(req_soc_id)
print(soc_list)我得到的输出是:
a.xml
['1','2','3']
b.xml
[4,5,6]
c.xml
[7,8,9]我想把所有的都合并成一个列表
发布于 2017-09-13 17:26:26
据我分析,我认为您希望将所有soc_list值写入单个文件,然后可以将该文件读回。这样做对来说是最好的方法,因为您将不知道用户文件上传,就像您在问题中提到的那样。若要这样做,请尝试理解并实现以下代码以保存到您的文件中。
save_path = "your_path_goes_here"
name_of_file = "your_file_name"
completeName = os.path.join(save_path, name_of_file + ".txt")
file1 = open(completeName, 'a')
for soc_id in self.tree.iter(tag='SOC_ID'):
req_soc_id = soc_id.text
soc_list.append(req_soc_id)
file1.write(req_soc_id)
file1.write("\n")
file1.close()通过这种方式,您可以始终将数据写入文件,然后读取数据并将其转换为列表,如下所示
examplefile = open(fileName, 'r')
yourResult = [line.split('in_your_case_newline_split') for line in examplefile.readlines()]https://stackoverflow.com/questions/46202537
复制相似问题