[英]how to parse a file and write to an output file in python
我是python的新手。 我正在嘗試解析文件以提取某些列並寫入輸出文件。 我能夠解析並提取所需的列,但是將它們寫入輸出文件時遇到了麻煩。
這是原始測試文件:
EGW05759 Pld5 I79_005987 GO_function: GO:0003824 - catalytic activity [Evidence IEA]; GO_process: GO:0008152 - metabolic process [Evidence IEA]
EGW05760 Exo1 I79_005988 GO_function: GO:0003677 - DNA binding [Evidence IEA]; GO_function: GO:0003824 - catalytic activity [Evidence IEA]; GO_function: GO:0004518 - nuclease activity [Evidence IEA]; GO_process: GO:0006281 - DNA repair [Evidence IEA]
這是我的python代碼
f = open('test_parsing.txt', 'rU')
f1 = open('test_parsing_out.txt', 'a')
for line in f:
match = re.search('\w+\s+(\w+)\s+\w+\s+\w+\:', line)
match1 = re.findall('GO:\d+', line)
f1.write(match.group(1), match1)
f1.close()
基本上我希望輸出看起來像這樣(盡管我知道我的代碼還不完整)
Pld5 GO:0003824:GO:0008152
Exo1 GO:0003677:GO:0003824:GO:0004518:GO:0006281
謝謝
Upendra
f = open('test_parsing.txt', 'rU')
f1 = open('test_parsing_out.txt', 'a')
for line in f:
match = re.search('\w+\s+(\w+)\s+\w+\s+\w+\:', line)
match1 = re.findall('GO:\d+', line)
f1.write('%s %s \n'%(match.group(1), ''.join(match1)))
f1.close()
使用csv
模塊:
import csv, re
with open('test_parsing.txt', 'rU') as infile, open('test_parsing_out.txt', 'a') as outfile:
reader = csv.reader(infile, delimiter="\t")
for line in reader:
result = line[1] + " " + ':'.join(re.findall("GO:\d{6}", line[3]))
outfile.write(result + "\n")
# OUTPUT
Pld5 GO:000382:GO:000815
Exo1 GO:000367:GO:000382:GO:000451:GO:000628
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.