使用Python3.3。我想做以下工作:
这就是我目前所拥有的:
mystring_modified = mystring.replace('\u00E9', 'e').replace('\u00F4', 'o').lower()
alphnumspace = re.compile(r"[^a-zA-Z\d\s]")
mystring_modified = alphnumspace.sub('', mystring_modified)
我该如何改进?效率是一个很大的问题,特别是因为我目前正在执行循环内的操作:
# Pseudocode
for mystring in myfile:
mystring_modified = # operations described above
mylist.append(mystring_modified)
有问题的文件每个大约有200000个字符。
也可以查看unidecode
您可以使用str.translate:
收益率
在下面,您必须列出所有要翻译的特殊重音字符。@gnibbler的方法需要更少的编码。
从好的方面来说,
str.translate
方法应该相当快,并且一旦设置了table
,它可以在一个函数调用中处理所有的需求(下壳、删除和删除重音符号)。顺便说一下,一个有200K个字符的文件不是很大。因此,将整个文件读入单个
str
,然后在一个函数调用中转换它会更有效。相关问题 更多 >
编程相关推荐