【发布时间】:2008-10-06 17:11:30
【问题描述】:
我使用 3rd 方工具以 Unicode 格式输出文件。但是,我更喜欢它是 ASCII。该工具没有更改文件格式的设置。
使用 Python 转换整个文件格式的最佳方法是什么?
【问题讨论】:
标签: python unicode encoding file ascii
我使用 3rd 方工具以 Unicode 格式输出文件。但是,我更喜欢它是 ASCII。该工具没有更改文件格式的设置。
使用 Python 转换整个文件格式的最佳方法是什么?
【问题讨论】:
标签: python unicode encoding file ascii
顺便说一句,这是一个 linux 命令iconv 来完成这种工作。
iconv -f utf8 -t ascii <input.txt >output.txt
【讨论】:
您可以使用unicode 函数轻松转换文件,但是如果没有直接的 ASCII 等效字符,您会遇到 Unicode 字符问题。
This blog 推荐@987654322@ 模块,它似乎可以处理没有直接对应ASCII值的粗略转换字符,例如
>>> title = u"Klüft skräms inför på fédéral électoral große"
通常转换为
Klft skrms infr p fdral lectoral groe
这是非常错误的。但是,使用unicodedata 模块,结果可以更接近原文:
>>> import unicodedata
>>> unicodedata.normalize('NFKD', title).encode('ascii','ignore')
'Kluft skrams infor pa federal electoral groe'
【讨论】:
unicodedata 之上构建的用于特殊情况替换的简单脚本:effbot.org/zone/unicode-convert.htm
我认为这是一个比你意识到的更深层次的问题。简单地将文件从 Unicode 更改为 ASCII 很容易,但是,将所有 Unicode 字符转换为合理的 ASCII 对应字符(许多字母在两种编码中都不可用)是另一回事。
本 Python Unicode 教程可能会让您更好地了解转换为 ASCII 的 Unicode 字符串会发生什么:http://www.reportlab.com/i18n/python_unicode_tutorial.html
以下是来自网站的有用引用:
Python 1.6 也有一个“unicode” 内置功能,您可以 指定编码:
> >>> unicode('hello') u'hello'
> >>> unicode('hello', 'ascii') u'hello'
> >>> unicode('hello', 'iso-8859-1') u'hello'
> >>>
所有这三个返回相同 事情,因为“你好”中的字符 对所有三种编码都是通用的。
现在让我们用 欧洲口音,不在 ASCII。你在控制台看到的可能 取决于您的操作系统 语言环境; Windows 让我输入 ISO-Latin-1。
> >>> a = unicode('André','latin-1')
> >>> a u'Andr\202'
如果您不能输入锐角字母 e, 你可以输入字符串'Andr\202', 这是明确的。
Unicode 支持所有常见的 迭代等操作 分裂。我们不会碾压他们 在这里。
【讨论】:
这里有一些简单(愚蠢)的代码来进行编码转换。我假设(但您不应该)输入文件是 UTF-16 格式(Windows 将其简单地称为“Unicode”)。
input_codec = 'UTF-16'
output_codec = 'ASCII'
unicode_file = open('filename')
unicode_data = unicode_file.read().decode(input_codec)
ascii_file = open('new filename', 'w')
ascii_file.write(unicode_data.write(unicode_data.encode(output_codec)))
请注意,如果 Unicode 文件中有任何字符不是 ASCII 字符,这将不起作用。您可以执行以下操作将无法识别的字符变成'?':
ascii_file.write(unicode_data.write(unicode_data.encode(output_codec, 'replace')))
查看the docs 以获得更简单的选择。如果您需要做一些更复杂的事情,您可能希望查看 Python Cookbook 中的The UNICODE Hammer。
【讨论】:
像这样:
uc = open(filename).read().decode('utf8')
ascii = uc.decode('ascii')
但请注意,如果有任何字符无法转换为 ASCII,这将失败并出现UnicodeDecodeError 异常。
编辑:正如 Pete Karl 刚刚指出的,从 Unicode 到 ASCII 没有一对一的映射。因此,某些字符根本无法以保留信息的方式进行转换。此外,标准 ASCII 或多或少是 UTF-8 的子集,因此您甚至不需要进行任何解码。
【讨论】:
对于我只想跳过非 ascii 字符而只输出 ascii 输出的问题,以下解决方案效果非常好:
import unicodedata
input = open(filename).read().decode('UTF-16')
output = unicodedata.normalize('NFKD', input).encode('ASCII', 'ignore')
【讨论】:
请务必注意,没有“Unicode”文件格式。 Unicode 可以通过几种不同的方式编码为字节。最常见的是 UTF-8 或 UTF-16。您需要知道您的第 3 方工具输出的是哪一个。一旦你知道了,不同编码之间的转换就很容易了:
in_file = open("myfile.txt", "rb")
out_file = open("mynewfile.txt", "wb")
in_byte_string = in_file.read()
unicode_string = bytestring.decode('UTF-16')
out_byte_string = unicode_string.encode('ASCII')
out_file.write(out_byte_string)
out_file.close()
正如其他回复中所述,您可能希望为 encode 方法提供一个错误处理程序。使用“替换”作为错误处理程序很简单,但如果文本包含无法用 ASCII 表示的字符,则会破坏您的文本。
【讨论】:
正如其他海报所指出的,ASCII 是 unicode 的子集。
但是如果你:
那么下面的例子展示了怎么做:
mystring = u'bar'
type(mystring)
<type 'unicode'>
myasciistring = (mystring.encode('ASCII'))
type(myasciistring)
<type 'str'>
【讨论】: