Python - 将utf8特殊字符(重音)转换为扩展的ascii等效项

时间:2018-06-14 08:30:05

标签: python utf-8 ascii

我想使用Python将utf8特殊字符(重音等)转换为扩展的ascii(纯粹主义者会说没有这样的东西,所以我的意思是link )相当于。

所以基本上我想读一个UTF-8文件并写出一个扩展的ascii文件(比如Latin-1(我正在使用windows),如果需要那些信息的话。我已经阅读了所有的Unicode等等。博客,但仍然不理解它的一个词),但我想尽可能多地保留信息。所以对于UTF-8字符á我想将它转换为扩展的ascii等效á。我不想忽略或松散角色,我不想使用a。对于没有等效的扩展ascii字符的字符,我只想使用我选择的字符,例如〜,尽管有些字符如ß我想转换为ss,如果在扩展的ascii中不存在ß。

Python 3中是否有任何可以执行此操作的内容,或者您​​可以提供一些示例代码来说明我将如何执行此操作?

有没有人知道任何列出扩展ascii字符的utf8等价物的网站?

基于下面的评论,我提出了这个代码,遗憾的是,由于大多数特殊字符都被返回,因此很可能效果不佳?而不是ê(不确定为什么):

# -*- coding: utf-8 -*-

f_in = open(r'E:/work/python/lyman.txt', 'rU', encoding='utf8')
raw = f_in.read()

f_out = open(r'E:/work/python/lyman_ascii.txt', 'w', encoding='cp1252', errors='replace')

retval = []
for char in raw:
    codepoint = ord(char)
    if codepoint < 0x80: # Basic ASCII
        retval.append(str(char))
        continue
    elif codepoint > 0xeffff:
        continue # Characters in Private Use Area and above are ignored
    # ë
    elif codepoint == 235:
        retval.append(chr(137))
        continue
    # ê
    elif codepoint == 234:
        retval.append(chr(136))
        continue
    # ’
    elif codepoint == 8217:
        retval.append(chr(39)) # 146 gives ? for some reason
        continue
    else:
        print(char)
        print(codepoint)

print(''.join(retval))
f_out.write(''.join(retval))

1 个答案:

答案 0 :(得分:0)

这似乎有效:

# -*- coding: utf-8 -*-
import sys

# Don't use codecs in Python 3.
f_in = open(r'af_massaged.txt', 'rU', encoding='utf8')
raw = f_in.read()

f_out = open(r'af_massaged_ascii.txt', 'w', encoding='cp1252', errors='replace')

retval = []
for char in raw:
    codepoint = ord(char)
    if codepoint < 0x80:    # Basic ASCII.
        retval.append(str(char))
        continue
    elif codepoint > 0xeffff:
        continue    # Characters in Private Use Area and above are ignored.
    elif codepoint >= 128 and codepoint <= 159:
        continue    # Ignore control characters in Latin-1.
    # Don't use unichr in Python 3, chr uses unicode. Get character codes from here: https://en.wikipedia.org/wiki/List_of_Unicode_characters#Latin-1_Supplement
    # This was written on Windows 7 32 bit
    # For 160 to 255 Latin-1 matches unicode.
    elif codepoint >= 160 and codepoint <= 255:
        retval.append(str(char))
        continue
    # –
    elif codepoint == 8211:
        retval.append(chr(45))
        continue
    # ’
    elif codepoint == 8217:
        retval.append(chr(180)) # 39
        continue
    # “
    elif codepoint == 8220:
        retval.append(chr(34))
        continue
    # ”
    elif codepoint == 8221:
        retval.append(chr(34))
        continue
    # €
    elif codepoint == 8364:
        retval.append('Euro')
        continue
    # Find missing mappings.
    else:
        print(char)
        print(codepoint)

# Uncomment for debugging.
#for i in range(128, 256):
#    retval.append(str(i) + ': ' + chr(i) + chr(13))

#print(''.join(retval))
f_out.write(''.join(retval))