如何在Python中分割多语言行并获取Unicode十六进制值?

发布于 2024-12-24 23:25:59 字数 693 浏览 3 评论 0原文

我尝试在 Python 中拆分此类行:

aiburenshi爱不忍释“לא מסוגל להננתק, לא כול להйפרד מדבר מרוב חйבתו אלו”

此行包含希伯来文、简体中文和英文。

例如,如果我有一个元组 T,我希望该元组为 T=(希伯来语字符串,英语字符串,中文字符串)。

问题是我不知道如何获取希伯来字母的中文的Unicode值。这两行都不起作用:

print ((unicode("释","utf-8")).encode("utf-8"))
print ((unicode("א","utf-8")).encode("utf-8"))

我收到此错误:

语法错误:文件 split_or.py 第 9 行中存在非 ASCII 字符“\xe9”,但未声明编码;请参阅http://www.python.org/peps/pep-0263.html 详情

I try to split this kind of lines in Python:

aiburenshi 爱不忍释 "לא מסוגל להינתק, לא יכול להיפרד מדבר מרוב חיבתו אליו"

This line contains Hebrew, simplified Chinese and English.

If I have a tuple T for example, I would like to get the tuple to be T= (Hebrew string, English string, Chinese string).

The problem is that I don't figure out how to get the Unicode value of the Chinese of the Hebrew letters. Both these lines don't work:

print ((unicode("释","utf-8")).encode("utf-8"))
print ((unicode("א","utf-8")).encode("utf-8"))

And I get this error:

SyntaxError: Non-ASCII character '\xe9' in file split_or.py on line 9, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

汐鸠 2024-12-31 23:25:59

在 Python 2 中,您需要打开指定编码的文件,如下所示:

import codecs
f = codecs.open("myfile.txt","r",encoding="utf-8") 

在 Python 3 中,您只需将编码选项添加到任何 open() 调用中即可。

这将保证文件被正确解码。请注意,这并不意味着您的打印调用将正常工作,这取决于很多因素(例如,请参见 http://www.pycs.net/users/0000323/stories/14.html 这只是一个开始);最好使用适当的调试器,或输出到文件(将再次使用 codecs.open() 打开)。

要获取实际的代码点(即整数“值”),您可以使用内置的 ord():

>>> ord(u"£")
163

如果您知道不同语言的范围,这就是您所需要的。请参阅此页面此页面了解范围。

否则,您可能需要使用 unicodedata 来查找内容,例如双向类别:

>>> unicodedata.bidirectional(u"£")
ET  # 'E'uropean 'T'erminator

In Python 2, you need to open the file specifying an encoding like this:

import codecs
f = codecs.open("myfile.txt","r",encoding="utf-8") 

In Python 3, you can just add the encoding option to any open() calls.

This will guarantee that the file is correctly decoded. Note that this doesn't mean your print calls will work properly, that depends on many things (see for example http://www.pycs.net/users/0000323/stories/14.html and that's just a start); it's better to either use a proper debugger, or output to a file (which will again be opened with codecs.open() ).

To get the actual codepoint (i.e. integer "value"), you can use the built-in ord():

>>> ord(u"£")
163

if you know the ranges for different languages, that's all you need. See this page or this page for the ranges.

Otherwise, you might want to use unicodedata to look up stuff, like the bidirectional category:

>>> unicodedata.bidirectional(u"£")
ET  # 'E'uropean 'T'erminator
旧人九事 2024-12-31 23:25:59

在 Python 2 中,Unicode 字符串常量需要以“u”字符开头,如下所示:

print ((unicode(u"释","utf-8")).encode("utf-8"))
print ((unicode(u"א","utf-8")).encode("utf-8"))

在 Python 3 中,字符串常量默认为 Unicode。

In Python 2, Unicode string constants need to be prefaced with the "u" character, as in:

print ((unicode(u"释","utf-8")).encode("utf-8"))
print ((unicode(u"א","utf-8")).encode("utf-8"))

In Python 3, string constants are Unicode by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文