python如何从行中提取符合的信息,正则表达式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python如何从行中提取符合的信息,正则表达式相关的知识,希望对你有一定的参考价值。
原文件是txt文件,内容
<SAM_Alignment object: Read'SN7001323:485:HFKWJBCXX:1:2101:15087:23492' aligned to chr1:[14499,145523)/->
<SAM_Alignment object:Read'SN7001588:485:HFKWJBCXX:1:2108:1370:11470' aligned to chr1:[14499,14527)/->
<SAM_Alignment object: Read 'SN7001388:481:HFKWJBCXX:1:2211:2581:68914' aligned to chr1:[14501,14487)/->
<SAM_Alignment object: Read 'SN7001312:435:HFKWJBCXX:1:1104:13571:17742' aligned to chr1:[14419,14450)/->
<SAM_Alignment object: Read 'SN7001488:486:HFKWJBCXX:1:1104:18532:23604' aligned to chr1:[14445,14470)/->
想从中输出的是 SNSN7001488:486:HFKWJBCXX:1:1104:18532:23604 chr1:14470--14490 就是说要序列号和chr和最后那个数字和最后那个数字加20
file_object = open('temp.txt')
try:
str = file_object.read( )
finally:
file_object.close( )
result = re.findall("(\d+%) S\s+\d+ (\d+)K\s+(\d+)K",str)
f = open("test.csv","w")
for line in result:
f.write("%s,%s,%s\n"%(line[0],line[1],line[2]))
f.close()
以上是关于python如何从行中提取符合的信息,正则表达式的主要内容,如果未能解决你的问题,请参考以下文章