Convert unicode small capitals to their ASCII equivalents

时间:2019-04-16 22:04:23

标签: python unicode ascii python-unicode

I have the following dataset

'Fʀɪᴇɴᴅ',
 'ᴍᴏᴍ',
 'ᴍᴀᴋᴇs',
 'ʜᴏᴜʀʟʏ',
 'ᴛʜᴇ',
 'ᴄᴏᴍᴘᴜᴛᴇʀ',
 'ʙᴇᴇɴ',
 'ᴏᴜᴛ',
 'ᴀ',
 'ᴊᴏʙ',
 'ғᴏʀ',
 'ᴍᴏɴᴛʜs',
 'ʙᴜᴛ',
 'ʟᴀsᴛ',
 'ᴍᴏɴᴛʜ',
 'ʜᴇʀ',
 'ᴄʜᴇᴄᴋ',
 'ᴊᴜsᴛ',
 'ᴡᴏʀᴋɪɴɢ',
 'ғᴇᴡ',
 'ʜᴏᴜʀs',
 'sᴏᴜʀᴄᴇ',

I want then into ASCII format using Python script for example:

Fʀɪᴇɴᴅ - FRIEND
ᴍᴏᴍ - MOM

I have tried encoding decoding but that doesn't work i also have tried this solution. but that doesn't solve my problem.

1 个答案:

答案 0 :(得分:1)

Python没有提供将small caps字符直接转换为ASCII等价字符的方法。但是,可以使用str.translate来做到这一点。

要使用str.translate,我们需要创建一个小写字母字符序数到ASCII字符的映射。

要获取序数值,我们可以构造每个字符的名称,然后从unicodedata数据库中获取字符并在其上调用ord。请注意,没有大写字母“ X”,在3.7之前的Python版本中,不存在小写字母“ Q”。

>>> from string import ascii_uppercase
>>> import unicodedata as ud

>>> # Filter out unsupported characters
>>> # Python < 3.7
>>> letters = (x for x in ascii_uppercase if x not in ('Q', 'X'))
>>> # Python >= 3.7
>>> letters = (x for x in ascii_uppercase if x != 'X') 

>>> mapping = {ord(ud.lookup('LATIN LETTER SMALL CAPITAL ' + x)): x for x in letters}

一旦有了映射,我们就可以使用它来为str.translatestr.maketrans创建转换表,然后执行转换。

>>> # Make as translation table
>>> tt = str.maketrans(mapping)
>>> # Use the table to "translate" strings to their ASCII equivalent.
>>> s = 'ᴍᴏɴᴛʜ'
>>> s.translate(tt)
'MONTH'
相关问题