我试图使用Python以操纵从格式一个文本文件:使用Python来操纵的键值分组txt文件演示
Key1
Key1value1
Key1value2
Key1value3
Key2
Key2value1
Key2value2
Key2value3
Key3...
给format B:
Key1 Key1value1
Key1 Key1value2
Key1 Key1value3
Key2 Key2value1
Key2 Key2value2
Key2 Key2value3
Key3 Key3value1...
具体而言,这里是一个简单介绍一下文件本身(只显示了一个键,数千人在完整的文件):
chr22:16287243: PASS
patientID1 G/G
patientID2 G/G
patient ID3 G/G
而且所需要的输出的位置:
chr22:16287243: PASS patientID1 G/G
chr22:16287243: PASS patientID2 G/G
chr22:16287243: PASS patientID3 G/G
我写以下代码可检测/显示键,但我无法编写代码来存储与每个键相关联的值,并且随后印刷这些键 - 值对。任何人都可以请这个任务协助我吗?
import sys
import re
records=[]
with open('filepath', 'r') as infile:
for line in infile:
variant = re.search("\Achr\d",line, re.I) # all variants start with "chr"
if variant:
records.append(line.replace("\n",""))
#parse lines until a new variant is encountered
for r in records:
print (r)
我不得不改变打印语句是如何被格式化了一点,但现在它的伟大工程!我也不知道“startswith”,所以也谢谢你:) – alexhli