文件名中的奇怪字符
Strange Characters in filename
我正在尝试通过 RestAPI 将附件从一个合流页面复制到 python 3.9 中的另一个合流页面。
在这样做的同时,我发现了一个 docx 文件,它的文件名中有一些奇怪的字符。
Downloadlink to File
文件名如下:Template_AnfrageEingangsbestätigung.docx
如果我要删除字符 'ä',它会这样做:Template_Anfrage Eingangsbestatigung.docx
我希望这样:Template_Anfrage Eingangsbesttigung.docx
你能告诉我是什么导致了这个问题吗?
如果您能告诉我如何将这些字符转换为普通的 utf-8 字符,那就太棒了。
抱歉我的英语不好。抱歉,如果这是一个愚蠢的问题。我是一个绝对的初学者,我没有在网上找到解决方案,因为我真的不知道要搜索什么。
The 'ä' on mac (ä
) is different to the 'ä' on windows (ä
)
您的问题并非源于 OS 差异(Mac 与 Windows) ;它是关于 Unicode normalization 的,请参阅以下脚本及其输出:
import unicodedata
def printref( phase, strings ):
global origins
linetemplate = '{0:<10} {1:<4} {2:4} {3:4} {4:4} {5}'
print( '' )
print( chr(0x20)*10, phase.ljust(9,chr(0x20)), strings[0]==strings[1] )
for ii, chars in enumerate( strings):
print( linetemplate.format( origins[ii], len(chars), chars,
chars.encode('utf-8').decode('cp1252'), # mojibake
'', ''
))
for char in chars:
print( linetemplate.format( '', len(char), char,
char.encode('utf-8').decode('cp1252'), # mojibake
unicodedata.category(char),
unicodedata.name(char,'???') ) )
strings = ['ä', 'ä']
origins = ['filename', 'question']
printref( 'original', strings)
for form in ['NFKC', 'NFKD']:
printref( form, [ unicodedata.normalize(form, x) for x in strings] )
输出:.\SO919847.py
original False
filename 2 ä ä
1 a a Ll LATIN SMALL LETTER A
1 ̈ ̈ Mn COMBINING DIAERESIS
question 1 ä ä
1 ä ä Ll LATIN SMALL LETTER A WITH DIAERESIS
NFKC True
filename 1 ä ä
1 ä ä Ll LATIN SMALL LETTER A WITH DIAERESIS
question 1 ä ä
1 ä ä Ll LATIN SMALL LETTER A WITH DIAERESIS
NFKD True
filename 2 ä ä
1 a a Ll LATIN SMALL LETTER A
1 ̈ ̈ Mn COMBINING DIAERESIS
question 2 ä ä
1 a a Ll LATIN SMALL LETTER A
1 ̈ ̈ Mn COMBINING DIAERESIS
不幸的是,我的浏览器以相同的方式呈现所有 ä
和 ä
;下图更能说明区别:
我正在尝试通过 RestAPI 将附件从一个合流页面复制到 python 3.9 中的另一个合流页面。 在这样做的同时,我发现了一个 docx 文件,它的文件名中有一些奇怪的字符。 Downloadlink to File
文件名如下:Template_AnfrageEingangsbestätigung.docx
如果我要删除字符 'ä',它会这样做:Template_Anfrage Eingangsbestatigung.docx
我希望这样:Template_Anfrage Eingangsbesttigung.docx
你能告诉我是什么导致了这个问题吗? 如果您能告诉我如何将这些字符转换为普通的 utf-8 字符,那就太棒了。
抱歉我的英语不好。抱歉,如果这是一个愚蠢的问题。我是一个绝对的初学者,我没有在网上找到解决方案,因为我真的不知道要搜索什么。
The 'ä' on mac (
ä
) is different to the 'ä' on windows (ä
)
您的问题并非源于 OS 差异(Mac 与 Windows) ;它是关于 Unicode normalization 的,请参阅以下脚本及其输出:
import unicodedata
def printref( phase, strings ):
global origins
linetemplate = '{0:<10} {1:<4} {2:4} {3:4} {4:4} {5}'
print( '' )
print( chr(0x20)*10, phase.ljust(9,chr(0x20)), strings[0]==strings[1] )
for ii, chars in enumerate( strings):
print( linetemplate.format( origins[ii], len(chars), chars,
chars.encode('utf-8').decode('cp1252'), # mojibake
'', ''
))
for char in chars:
print( linetemplate.format( '', len(char), char,
char.encode('utf-8').decode('cp1252'), # mojibake
unicodedata.category(char),
unicodedata.name(char,'???') ) )
strings = ['ä', 'ä']
origins = ['filename', 'question']
printref( 'original', strings)
for form in ['NFKC', 'NFKD']:
printref( form, [ unicodedata.normalize(form, x) for x in strings] )
输出:.\SO919847.py
original False
filename 2 ä ä
1 a a Ll LATIN SMALL LETTER A
1 ̈ ̈ Mn COMBINING DIAERESIS
question 1 ä ä
1 ä ä Ll LATIN SMALL LETTER A WITH DIAERESIS
NFKC True
filename 1 ä ä
1 ä ä Ll LATIN SMALL LETTER A WITH DIAERESIS
question 1 ä ä
1 ä ä Ll LATIN SMALL LETTER A WITH DIAERESIS
NFKD True
filename 2 ä ä
1 a a Ll LATIN SMALL LETTER A
1 ̈ ̈ Mn COMBINING DIAERESIS
question 2 ä ä
1 a a Ll LATIN SMALL LETTER A
1 ̈ ̈ Mn COMBINING DIAERESIS
不幸的是,我的浏览器以相同的方式呈现所有 ä
和 ä
;下图更能说明区别: