我正试图从文本文件中收集数据。当我打印输出时,它们会返回我正在查找的正确值,但是,当我尝试使用xlsxwriter将这些输出放入表中时,该表只包含一个文本文件最后一行的输出,重复了文本文件中有行的次数。 i、 e.有5000行文本,我需要从中获取3条信息,.xlsx文件有5000行和3列,但都包含文本文件中最后一行的信息。你知道吗
EC:1>;GO:N-乙基马来酰亚胺还原酶活性;GO:0008748
EC:1>;GO:氧化还原酶活性;GO:0016491
EC:1>;GO:辅酶F420脱氢酶活性降低;GO:0043738
EC:1>;GO:硫氧合酶还原酶活性;GO:0043826
EC:1>;GO:苹果酸-乳酸酶活性;GO:0043883
^txt文件的外观
6.6.1.2钴胆碱酯酶活性0051116
6.6.1.2钴胆碱酯酶活性0051116
6.6.1.2钴胆碱酯酶活性0051116
6.6.1.2钴胆碱酯酶活性0051116
6.6.1.2钴胆碱酯酶活性0051116
6.6.1.2钴胆碱酯酶活性0051116
6.6.1.2钴胆碱酯酶活性0051116
6.6.1.2钴胆碱酯酶活性0051116
6.6.1.2钴胆碱酯酶活性0051116
6.6.1.2钴胆碱酯酶活性0051116
。。。你知道吗
(除了5000行,桌子看起来如何)
任何帮助都将不胜感激, 敬礼
import xlsxwriter
File = 'EC_to_GO.txt'
def analysis(line, output):
with open(File) as fp:
lines = fp.readlines()
for line in lines:
output[0] = line[3:].split(' > ')[0]
output[1] = line[:-14].split(' > GO:')[-1]
output[2] = line[-8:]
return output
with open(File) as fp:
lines = fp.readlines()
for line in lines:
if 'Generated on 2018-07-04T09:08Z' in line:
a = lines.index(line)
for line in lines:
if 'GO:cobaltochelatase activity ; GO:0051116' in line:
b = lines.index(line)
req_list = lines[a:b]
rxn_end_index = []
for i in range(len(req_list)):
if '> GO:' in req_list[i]:
rxn_end_index.append(i)
inner_list = []
outer_list =[]
spare = [0] + rxn_end_index
for i in range(len(spare)-1):
inner_list = req_list[spare[i]:spare[i+1]]
outer_list.append(inner_list)
res_list=[]
for i in range(len(outer_list)):
res_list.append(analysis(outer_list[i],['NA','NA','NA']))
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('EC_to_GO.xlsx')
worksheet = workbook.add_worksheet('EC_to_GO')
#res_list1 = [EC, Genome name, GO]
#for i in res_list:
#res_list1.append(i)
# Some data we want to write to the worksheet.
t = tuple(res_list)
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for a,b,c in (t):
worksheet.write(row, col, a)
worksheet.write(row, col + 1, b)
worksheet.write(row, col + 2, c)
row += 1
workbook.close()
您基本上是将相同的列表附加到
res_list
。所以您有相同output
列表的多个副本。你知道吗要修复: 而不是
更改为:
或者
符号列表[:]创建列表的副本。从技术上讲,您正在创建整个列表的一部分。你知道吗
相关问题 更多 >
编程相关推荐