我有一个unicode文件路径列表,我需要用英语变音符替换所有变音符号。例如,我会用ue,ü用ae等等。我已经定义了变音符(键)和它们的变音符(值)的字典。所以我需要将每个密钥与每个文件路径以及密钥的位置进行比较,并将其替换为值。这看起来似乎很简单,但我无法让它工作。有没有人有任何想法?任何反馈非常感谢!Python - 音译德语变音拨号到Diacritic
到目前为止的代码:
# -*- coding: utf-8 -*-
import os
def GetFilepaths(directory):
"""
This function will generate all file names a directory tree using os.walk.
It returns a list of file paths.
"""
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_paths.append(filepath)
return file_paths
# dictionary of umlaut unicode representations (keys) and their replacements (values)
umlautDictionary = {u'Ä': 'Ae',
u'Ö': 'Oe',
u'Ü': 'Ue',
u'ä': 'ae',
u'ö': 'oe',
u'ü': 'ue'
}
# get file paths in root directory and subfolders
filePathsList = GetFilepaths(u'C:\\Scripts\\Replace Characters\\Umlauts')
for file in filePathsList:
for key, value in umlautDictionary.iteritems():
if key in file:
file.replace(key, value) # does not work -- umlauts still in file path!
print file
更换不修改它会返回修改过的字符串... –
[为什么不调用Python字符串方法会执行任何操作,除非分配它的输出?](http:// stackover flow.com/faqs/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out) –
我不确定适当的词是什么,但“变音符“是指用于标记变音符号的两个点,而不是两个字母的拼写替代。 – chepner