pyparsing的作者Paul McGuire是kind enough to help a lot with a problem I'm trying to solve。我們在第一場比賽中打入了一球,但我甚至無法將球投進球門線。孔子說,如果他給了一個學生1/4的解決方案,並且他沒有與其他3/4一起回來,那麼他不會再教這個學生。所以這是經過近一個星期的沮喪和非常焦慮,我問這個...python&pyparsing newb:如何打開文件
如何打開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()
http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files – monkut