【问题标题】:Python - Convert utf8 special characters (accented) to extended ascii equivalentPython - 将 utf8 特殊字符(重音)转换为扩展的 ascii 等效字符
【发布时间】:2018-11-23 23:12:08
【问题描述】:

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

所以基本上我想读入一个 UTF-8 文件并写出一个扩展的 ascii 文件(如果需要该信息,类似于 Latin-1(我正在使用 Windows)。我已经阅读了所有的 Unicode 等。博客,但一个字也听不懂),但我想尽可能多地保留信息。因此,对于 UTF-8 字符 á,我想将其转换为扩展的 ascii 等价 á。我不想忽略或失去角色,也不想使用 a。对于没有等效扩展 ascii 字符的字符,我只想使用我选择的字符,例如 ~,尽管如果扩展 ascii 中不存在 ß,我想将某些字符(如 ß)转换为 ss。

在 Python 3 中有什么可以做到这一点,或者你能给出一些我将如何做到这一点的示例代码吗?

有谁知道列出扩展 ascii 字符的 utf8 等效项的任何网站?

根据下面的 cmets,我想出了这段代码,遗憾的是,由于大多数特殊字符都返回为 ?而不是ê(不知道为什么):

# -*- 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))

【问题讨论】:

  • 你的意思是code page 850
  • 可能是的。我使用 Windows,我来自南非,我们使用英国英文字符。
  • 要写入 CP-850 中的文件,请使用 open(filename, 'w', encoding='cp850', errors='replace');这将用“?”替换不可表示的字符。对于将 'ß' 转换为 'ss' 等,您可以使用第三方库 unidecode,但它也会将“á”替换为“a”——输出是纯 ASCII。如果您想将两者结合起来,则需要构建自己的解决方案。
  • 你的建议正是我不想做的,因此我的问题是关于如何构建自己的解决方案。
  • 旁注:永远不要使用codecs.open;认为它“实际上已弃用”(它的存在主要是为了支持一些深奥的字节->字节/文本->文本编解码器)。在 Python 2.7 上,使用 io.open,在 Python 3.x 上,使用内置的 open(与 Python 3 上的 io.open 完全相同),两者都接受 encoding 参数,并且更高效、更正确地运作。

标签: python utf-8 ascii


【解决方案1】:

这似乎有效:

# -*- 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-16
    • 2010-10-12
    • 2017-03-30
    • 2013-01-26
    • 2012-08-02
    • 2012-11-18
    • 1970-01-01
    相关资源
    最近更新 更多