Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x str.format将最后一个变量放在打印的第一位_Python 3.x - Fatal编程技术网

Python 3.x str.format将最后一个变量放在打印的第一位

Python 3.x str.format将最后一个变量放在打印的第一位,python-3.x,Python 3.x,此脚本的目的是解析文本文件(sys.argv[1]),提取某些字符串,并在列中打印它们。我从打印标题开始。然后我打开文件,逐行扫描。我确保该行有一个特定的开始或包含一个特定的字符串,然后使用正则表达式提取特定的值。 匹配和提取工作正常 我的最终打印报表无法正常工作 import re import sys print("{}\t{}\t{}\t{}\t{}".format("#query", "target", "e-value", "identity(%)", "score")) wi

此脚本的目的是解析文本文件(sys.argv[1]),提取某些字符串,并在列中打印它们。我从打印标题开始。然后我打开文件,逐行扫描。我确保该行有一个特定的开始或包含一个特定的字符串,然后使用正则表达式提取特定的值。 匹配和提取工作正常

我的最终打印报表无法正常工作

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='')
当我尝试打印列时出现问题。当我打印前两列时,它会按预期工作(除了仍在打印新行):

第三列是科学记数法中的数字,如2e-34

但当我添加第三列eValue时,它会崩溃:

#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()方法删除了所有新行。

至少有三个问题:

1)
queryID.rstrip
target.rstrip
缺少关闭
()

2) 类似于
truevalue.rstrip()
的东西不会改变字符串,您需要

trueEValue = trueEValue.rstrip() 
如果你想保留零钱


3) 这可能是个问题,但如果没有看到您的数据,我无法100%确定。
rstrip
中的
r
表示“右”。如果
truevalue
4e-43\n
truevalue.rstrip()
将没有换行符。但问题是您的值似乎类似于
\n43-43
。如果您只需使用
.strip()
,则换行符将从两侧删除。

是的,就是这样!我总是忘记弦是多么难以改变。
trueEValue = trueEValue.rstrip()