在Python中解析带分隔符的行
我有一些数据行,想要对它们进行解析。数据的格式是这样的:
a score=216 expect=1.05e-06
a score=180 expect=0.0394
我想要做的是写一个小程序,能够解析这些数据,并为每一行返回两个值(分数和期望值)。
但是我写的这个函数似乎没有正常工作:
def scoreEvalFromMaf(mafLines):
for word in mafLines[0]:
if word.startswith("score="):
theScore = word.split('=')[1]
theEval = word.split('=')[2]
return [theScore, theEval]
raise Exception("encountered an alignment without a score")
请问正确的做法是什么呢?
3 个回答
1
这是一个强制性的,可能不太合适的正则表达式解决方案:
import re
def scoreEvalFromMaf(mafLines):
return [re.search(r'score=(.+) expect=(.+)', line).groups()
for line in mafLines]
2
如果 mafLines
是一组行的列表,而你只想看第一行,可以用 .split
方法把这一行拆分成单词。例如:
def scoreEvalFromMaf(mafLines):
theScore = None
theEval = None
for word in mafLines[0].split:
if word.startswith('score='):
_, theScore = word.partition('=')
elif word.startswith('expect='):
_, theEval = word.partition('=')
if theScore is None:
raise Exception("encountered an alignment without a score")
if theEVal is None:
raise Exception("encountered an alignment without an eval")
return theScore, theEval
注意,这样做会返回一个包含两个 字符串 的元组;如果你想得到一个整数和一个浮点数,比如说,你需要把最后一行改成
return int(theScore), float(theEval)
这样的话,如果其中任何一个字符串不符合它应该代表的类型,就会出现一个 ValueError 错误;如果两个字符串都有效,你就会得到一个包含两个数字的元组。
2
看起来你想把每一行按照空格分开,然后分别处理每一部分。如果mafLines是一个字符串(也就是说,它是从.readlines()
读取的一行内容):
def scoreEvalFromMafLine(mafLine):
theScore, theEval = None, None
for word in mafLine.split():
if word.startswith("score="):
theScore = word.split('=')[1]
if word.startswith("expect="):
theEval = word.split('=')[1]
if theScore is None or theEval is None:
raise Exception("Invalid line: '%s'" % line)
return (theScore, theEval)
你之前的做法是逐个字符地遍历第一行(因为它是一个字符串列表),而不是按照空格来分开。