pyparsing 的作者 Paul McGuire 对我正在尝试解决的问题提供了很多帮助。我们落后一码,距离球门一码,但我什至无法将球踢过球门线。孔子说,如果他给了一个学生1/4的答案,而他没有带着另外的3/4回来,那么他就不会再教那个学生了。因此,在经历了将近一周的挫败感和极大的焦虑之后,我才提出这个问题……
如何打开输入文件进行 pyparsing 并将输出打印到另一个文件?
这是我到目前为止所得到的,但这实际上是他的全部工作
from pyparsing import *
datafile = open( 'test.txt' )
# Backaus Nuer Form
num = Word(nums)
accessionDate = Combine(num + "/" + num + "/" + num)("accDate")
accessionNumber = Combine("S" + num + "-" + num)("accNum")
patMedicalRecordNum = Combine(num + "/" + num + "-" + num + "-" + num)("patientNum")
gleason = Group("GLEASON" + Optional("SCORE:") + num("left") + "+" + num("right") + "=" + num("total"))
patientData = Group(accessionDate + accessionNumber + patMedicalRecordNum)
partMatch = patientData("patientData") | gleason("gleason")
lastPatientData = None
# PARSE ACTIONS
def patientRecord( datafile ):
for match in partMatch.searchString(datafile):
if match.patientData:
lastPatientData = match
elif match.gleason:
if lastPatientData is None:
print "bad!"
continue
print "{0.accDate}: {0.accNum} {0.patientNum} Gleason({1.left}+{1.right}={1.total})".format(
lastPatientData.patientData, match.gleason
)
patientData.setParseAction(lastPatientData)
# MAIN PROGRAM
if __name__=="__main__":
patientRecord()