我有一个文本文件,看起来像一个小例子:
小例子:
Name sample1 sample2 sample3
A2M 9805.6 3646.8 1376.48
ACVR1C 20 37.8 20
ADAM12 197.8 120.96 31.28
我正在尝试重新组织数据并制作一个新文本文件,看起来像预期的输出:
预期输出:
Name Sample
A2M 9805.6
A2M 3646.8
A2M 1376.48
ACVR1C 20
ACVR1C 37.8
ACVR1C 20
ADAM12 197.8
ADAM12 120.96
ADAM12 31.28
实际上,(输入数据的)最后3列将包含在输出数据的第二列中,并且输入文件的第一列中的每个项目将重复3次(每个名称有3个样本)。
为此,我在python3中编写了以下代码:
def convert(input_file, output_file):
with open(input_file, 'r') as infile:
res = {}
line = infile.split()
res.keys = line[0]
res.values = line[2:]
outfile = open(output_file, "w")
for k, v in res.items():
outfile.write(str(k) + '\t'+ str(v) + '\n')
,但它不返回我想要的内容。你知道如何解决吗?
答案 0 :(得分:1)
您的代码中有一些问题。
首先,您还应该在var result = await HttpContext.AuthenticateAsync(OpenIdConnectDefaults.AuthenticationScheme);
var value = result.Properties.Items["id"];
语句中打开outfile
。其次,字典的with
和keys
是只读的。最后,您尝试分割不可能的整个文件。您想像这样循环所有行:
values
尽管您应该考虑将格式更改为def convert(input_file, output_file):
with open(input_file) as infile, open(output_file, "w") as outfile:
outfile.write("Name\tSample")
for line in infile:
values = line.split()
for value in values[1:]:
outfile.write(values[0] + "\t" + value + "\n")
并将其读取到数据框。
答案 1 :(得分:1)
尝试一下
d= {}
with open('file1.txt','r') as f: # Your file
header = next(f)
for i in f:
d.setdefault(i.split()[0],[]).extend(i.split()[1:])
with open('nflie1.txt','w') as f: # New file
f.write('Name Sample\n')
for k,v in d.items():
for el in v:
f.write('{} {}\n'.format(k,el))
输出:
Name Sample
A2M 9805.6
A2M 3646.8
A2M 1376.48
ACVR1C 20
ACVR1C 37.8
ACVR1C 20
ADAM12 197.8
ADAM12 120.96
ADAM12 31.28