該腳本的目的是解析文本文件(sys.argv [1]),提取某些字符串並將它們打印在列中。我首先打印標題。然後我打開文件,並逐行掃描。我確定該行有特定的開始或包含特定的字符串,然後我使用正則表達式來提取特定的值。 匹配和提取工作正常。str.format首先打印最後一個變量
我的最終打印聲明無法正常工作。
import re
import sys
print("{}\t{}\t{}\t{}\t{}".format("#query", "target", "e-value",
"identity(%)", "score"))
with open(sys.argv[1], 'r') as blastR:
for line in blastR:
if line.startswith("Query="):
queryIDMatch = re.match('Query= (([^ ])+)', line)
queryID = queryIDMatch.group(1)
queryID.rstrip
if line[0] == '>':
targetMatch = re.match('> (([^ ])+)', line)
target = targetMatch.group(1)
target.rstrip
if "Score = " in line:
eValue = re.search(r'Expect = (([^ ])+)', line)
trueEvalue = eValue.group(1)
trueEvalue = trueEvalue[:-1]
trueEvalue.rstrip()
print('{0}\t{1}\t{2}'.format(queryID, target, trueEvalue), end='')
當我嘗試打印列時發生問題。當我打印的第2列,它按預期工作(除非它仍然打印新的生產線):
#query target e-value identity(%) score
YAL002W Paxin1_129011
YAL003W Paxin1_167503
YAL005C Paxin1_162475
YAL005C Paxin1_167442
第三列是科學記數法像2E-34
的數字,不過我加第3列,安勤,它打破了:
#query target e-value identity(%) score
YAL002W Paxin1_129011
4e-43YAL003W Paxin1_167503
1e-55YAL005C Paxin1_162475
0.0YAL005C Paxin1_167442
0.0YAL005C Paxin1_73182
我已刪除了所有新線路,據我所知,使用rstrip()可以方法。
是的,就是這樣!我總是忘記如何固執的字符串改變。 – Glubbdrubb