用Python代替缩写词典 [英] Replace with abbreviations from dictionary using Python
本文介绍了用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 reader 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 reesplit('(\W +)',s))
print>> out,da
进程
一个示例trial.csv文件将是< p,
A,B,C,D
RNA,肺癌,15,生物素
RNA,肺癌, 15,生物素
RNA,乳腺癌,15,生物素
RNA,乳腺癌,15,生物素
RNA,肺癌,15,生物素
Sample Dictionary.csv:
rna,ribonucleic酸
rnd,根治性颈部清扫
rni,推荐营养摄入
rnp,核糖核蛋白
我的输出文件应该被'核糖核酸'代替'RNA'
解决方案
我认为这行 s = str(ss)
导致问题 - 创建的列表刚刚成为一个字符串!
:
$ b
def process(ss):
for ss in:
da =''。 (mydict.get(word,word)for reesplit('(\W +)',line))
print>> out,da
进程(ss)
I'm trying to replace words like 'rna' with 'ribonucleic acid' from a dictionary of abbreviations. I tried writing the following, but it doesn't replace the abbreviations.
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)
A sample trial.csv file would be
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
Sample Dictionary.csv:
rna,ribonucleic acid
rnd,radical neck dissection
rni,recommended nutrient intake
rnp,ribonucleoprotein
My output file should have 'RNA' replaced by 'ribonucleic acid'
解决方案
I think this line s = str(ss)
is causing the problem - the list that was created just became a string!
Try this instead:
def process(ss):
for line in ss:
da = ''.join( mydict.get( word, word ) for word in re.split( '(\W+)', line ) )
print >> out, da
process(ss)
这篇关于用Python代替缩写词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文