약어 사전에서 'rna'와 'ribonucleic acid'와 같은 단어를 대체하려고합니다. 다음 글을 써 보았지만 약어를 대체하지는 않습니다.Python을 사용하여 사전에서 약어로 바꾸기
import csv,re
outfile = open ("Dict.txt", "w")
with open('Dictionary.csv', mode='r') as infile:
reader = csv.reader(infile)
mydict = {rows[0]:rows[1] for rows in reader}
print >> outfile, mydict
out = open ("out.txt", "w")
ss = open ("trial.csv", "r").readlines()
s = str(ss)
def process(s):
da = ''.join(mydict.get(word, word) for word in re.split('(\W+)', s))
print >> out, da
process(s)
샘플 trial.csv 파일은
A,B,C,D
RNA,lung cancer,15,biotin
RNA,lung cancer,15,biotin
RNA,breast cancer,15,biotin
RNA,breast cancer,15,biotin
RNA,lung cancer,15,biotin
샘플 Dictionary.csv 것 :
내 출력 파일이 '리보 핵산' 로 대체 'RNA'를해야rna,ribonucleic acid
rnd,radical neck dissection
rni,recommended nutrient intake
rnp,ribonucleoprotein
당신이' 'rna'' 또는''RNA''를 교체하려고합니까? 그것들은 다른 문자열입니까? – abarnert
또한 실행 및 디버그 할 수있는 _complete_ - 2 행의'trial.csv' 및'line'' Dictionary.csv' (또는 코드의 하드 코드 된 값)를 제공해주십시오. 도움말에서 [MCVE] (http://stackoverflow.com/help/mcve)를 참조하십시오. – abarnert
@abarnert 'RNA'를 대체하려하지만 'rna'가 있습니다. 이 사건을 무시할 수있는 방법이 있습니까? 또한, 'corna'를 대체하기를 원하지 않습니다. (나는 그러한 단어가 존재하지 않는다는 것을 알고 있습니다. 그러나 그것이 일어나지 않도록 확실히하고 싶습니다.) 'coribonucleic acid'. – abn