【问题标题】:How to store non-english text?如何存储非英文文本?
【发布时间】:2016-05-01 13:06:18
【问题描述】:

我有一个文本文件。它由许多非英文字符组成。我想将此文件存储为数字序列,例如 ascii。

如何表示非英文字符?

>>> str(ord('x'))
'120'
>>> str(ord('ç'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found
>>> 

【问题讨论】:

  • 为什么要将其存储为数字序列?
  • 为机器学习技术创建数据集。

标签: python encoding character-encoding python-2.x


【解决方案1】:

您必须首先使用正确的编码方案 decode 它,然后您将获得该字符的序数值,因为 ord 返回 单字符 的整数值字符串:

>>> s = 'ç'
>>> s
'\xc3\xa7'
>>> print s
ç
>>> len(s)
2
>>> s.decode('utf-8')
u'\xe7'
>>> len(s.decode('utf-8'))
1
>>> ord(s.decode('utf-8'))
231

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 2015-10-01
    • 2016-11-07
    • 2014-05-21
    • 1970-01-01
    相关资源
    最近更新 更多