简体   繁体   English

在python中写出匹配的行

[英]Write out the matched lines in python

I am trying to print out the line of the matched pattern and write out the matched lines. 我试图打印出匹配模式的行并写出匹配的行。

The number of the matched line works fine, however, neither did Python write the content in the new file, nor did it raise an error message. 匹配行的编号很好,但是,Python也没有在新文件中写入内容,也没有引发错误消息。

#!/usr/bin/env python
import re
outputLineNumbers = open('OutputLineNumbers', 'w')
outputLine = open('OutputLine', 'w')
inputFile = open('z.vcf','r')
matchLines = inputFile.readlines()    


total = 0
for i in range(len(matchLines)):
    line = matchLines[i]
#print out the matched line number    
    if re.match('(\w+)\|(\d+)\|(\w+)\|AGTA(\d+)\.(\d)\|\s(0+\d+)\s\.\s(\w)\s(\w),(\w)', line):
        total += 1
        outputLineNumbers.write( str(i+1) + "\n" )
#WRITE out the matched line
    if line == ('(\w+)\|(\d+)\|(\w+)\|AGTA(\d+)\.(\d)\|\s(0+\d+)\s\.\s(\w)\s(\w),(\w)'):
        outputLine.write( line + "\n" )
print "total polyploid marker is : ", total


outputLineNumbers.close()
inputFile.close()
outputLine.close()

You tried to test if the line is equal to the pattern: 您试图测试该行是否等于模式:

if line == ('(\w+)\|(\d+)\|(\w+)\|AGTA(\d+)\.(\d)\|\s(0+\d+)\s\.\s(\w)\s(\w),(\w)'):

String equality does not magically invoke the regular expression engine when the string appears to contain a pattern, however. 但是,当字符串看起来包含模式时,字符串相等不会神奇地调用正则表达式引擎。

Remove the if line == test and just write out the matched line as part of the preceding if block: 删除if line == test并将匹配的行写出作为前面if块的一部分:

if re.match('(\w+)\|(\d+)\|(\w+)\|AGTA(\d+)\.(\d)\|\s(0+\d+)\s\.\s(\w)\s(\w),(\w)', line):
    total += 1
    outputLineNumbers.write( str(i+1) + "\n" )
    #WRITE out the matched line
    outputLine.write( line + "\n" )

Note that you can just loop over matchLines directly ; 请注意,您可以刚过循环matchLines 直接 ; use the enumerate() function to produce a running index here instead: 使用enumerate()函数来生成运行索引:

for i, line in enumerate(matchLines, 1):
    if re.match('(\w+)\|(\d+)\|(\w+)\|AGTA(\d+)\.(\d)\|\s(0+\d+)\s\.\s(\w)\s(\w),(\w)', line):
        total += 1
        outputLineNumbers.write("{}\n".format(i))

where i starts at 1, so there is no need to add 1 later on either. i从1开始,所以没有必要在以后添加1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM