我需要編寫一個程序來識別醫療記錄中的名字。我該如何替換可能包含前綴,後綴和首字母或名字的名稱,但不必每次都有上述所有內容。例如,我可以讓程序去識別史密斯博士,但不是史密斯博士。使用Python正則表達式替換
謝謝!
這裏的節目,我到目前爲止有:
# This program removes names and email addresses occurring in a given input file and saves it in an output file.
import re
def deidentify():
infilename = input("Give the input file name: ")
outfilename = input("Give the output file name: ")
infile = open(infilename,"r")
text = infile.read()
infile.close()
# replace names
nameRE = "(Ms\.|Mr\.|Dr\.|Prof\.) [A-Z](\.|[a-z]+) [A-Z][a-z]+"
deidentified_text = re.sub(nameRE,"**name**",text)
outfile = open(outfilename,"w")
print(deidentified_text, file=outfile)
outfile.close()
deidentify()
我認爲這在很大程度上取決於輸入文本文件的格式。你的數據是純文本文件嗎? – pyan 2015-04-03 18:18:15
需要去識別的不同可能性有哪些?有或沒有前綴?有或沒有縮寫?有沒有名字?只有姓氏?你的文件中是否有其他姓氏不應該改變? – 2015-04-03 18:21:14
Pyan:是的,一個txt文件 – Ellis5 2015-04-03 19:09:36