Python - 使用ASCII字符有效地替换文本文件中的字符

时间:2016-04-11 15:11:58

标签: python python-3.x

我有一组文本文件,我想用Python替换另一个字符中的某些字符。我想要替换的角色来自威尔士语,它们是有向图。它们是两个独立的字符,形成一个字母。

以下是威尔士的一些有向图和一些字符,可以替换为:

ch - ƒ (ASCII code 131)
dd - Π(ASCII code 140)
ff - ¤ (ASCII code 164)

我将使用的文本文件可能相当大(几GB),并且有8个有向图;总共需要24个替换字符来覆盖所有形式(ch,Ch,CH)。我想知道实施这些替换的有效和高效方法是什么?

更新:

我有一个工作(目前为止)的程序版本,该程序基于这个问题的答案:

replacing text in a file with Python

这是我的代码:

replacements = {'ch':'ƒ', 'Ch':'†', 'ff':'¤', 'FF':'¦', 'Dd':'•', 'll':'º', 'Ll':'¿'}
print("Input file location: ")
inLoc = input("> ")
print("Output file location: ")
outLoc = input("> ")

with open(inLoc, "r") as infile, open(outLoc, "w") as outfile:
    for line in infile:
        for src, target in replacements.items():
            line = line.replace(src, target)
        outfile.write(line)

输入文字:

Ydych Chi'n hoffi COFFI?

Dda de. Lle why ti llywelyn?

输出文字:

Ydyƒ †i'n ho¤i CO¦I?

•a de. ¿e why ti ºywelyn?

0 个答案:

没有答案