问题标签 [python-unicode]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
284 浏览

python - 在 python <= 2.7 中编写 unicode 程序

在 python <= 2.7 中编写 unicode 程序的一般指导方针是什么?即使它不包含 ASCII 范围之外的任何字符,在每个字符串前面加上 u 是否也是一种好习惯?

在处理 sqlite3 时,参数化查询会自动将 unicode 编码为 utf-8,还是需要手动完成?

在处理“字符串”字节时,应该将其保留为字符串对象还是解码为 un​​icode 字符串?(我相信这在大多数情况下会抛出异常)

如果出于某种原因我需要在代码中使用文字 unicode 字符,我可以在字符串中使用该字符,只要它是 unicode 字符串并且我在文件顶部声明了我的编码吗?

编辑:打印 unicode 字符串时,如何获取用户系统的语言环境以便正确编码?盲目地将所有内容编码为 utf-8 似乎是个坏主意,因为并非所有系统都支持它。编辑:我相信我想出了这个。可以使用语言环境来完成

编辑:这种编码实际上是隐式完成的吗?现在我很困惑。在linux上,我可以做到这一点

但在 Windows 上会发生这种情况

似乎 print 隐式地​​进行了转换......

编辑:这个问题说 print 将自动编码为存储在 sys.stdout.encoding 中的编码 为什么 Python 在默认编码为 ASCII 时打印 unicode 字符?

现在我想知道,有没有办法让 print 的默认行为来替换不可编码的字符?或者我是否需要将 print 包装在我自己的函数中,例如:

我知道大多数这些问题已经在 Python 3 中得到解决,但我想支持 python <= 2.7。

0 投票
1 回答
280 浏览

python - Python unicode 错误

有人可以解释一下为什么在下面的示例中print a 会引发异常,而a.__str__()不会?

0 投票
1 回答
1832 浏览

python - 将“\uxxxxxx”形式的 unicode 转换为字符串或文本

我正在编写一个 python 脚本,它将提取 facebook 视频的 url。但是在视频页面的源代码中,我在 url 中看到了一些形式为 \uxxxxxx 的字符。

例如 url 就是这种形式

\u00253A 是冒号 (:),但我如何转换它。

当我喜欢

我明白了

我想要确切的网址而不是百分比。

我搜索了很多,但找不到任何帮助。

提前致谢

编辑

如果我传递 facebook 页面的整个源代码,然后将所有这些复杂的 unicode 字符转换为简单的字符,有什么办法吗?

0 投票
1 回答
422 浏览

python - unicode 和 python 问题(访问 unicode 代码图表)

昨天我写了以下function转换integerPersian

当你调用它如: integerToPersian(3455), it return ۳۴۵۵, ۳۴۵۵等价于3455inPersianArabic language. 当你读取一个数字如read from databae, 并想显示in 时widget,这 function非常有用。

我从http://unicode.orgcodes charts下载 的,因为我需要写根据它应该作为参数和存储,我也是 pytho 的新手。unicodePersianToInteger('unicodeString')utf-8utf-82 bytes

我的问题是,如何存储2bytes?,如何utf8存储,如何拆分unicode string为另一种格式?怎么能用unicode code charts

笔记:我发现可以使用int() built-in fuinction,但我不能使用它。也许你可以

0 投票
3 回答
2210 浏览

python - Converting Unicode Values as String from a Python Dictionary

I've built a python dictionary as follows:

which takes the name of the fields in each table (feature class) and sets tyhem as the key value in the dictionary and goes on to set the name of the table as the value. This works fine because my goal is to find out which tables have the same fields. I can go on to iter over the items and print out the key, value pairs:

which returns:

COMMENTS: [u'TM_FC', u'GT_FC', u'HG_FC', u'PO_FC', u'FU_FC']

I want to print out the key values as a string value instead of the unicode stuff so that it looks like this:

COMMENTS: 'TM_FC', 'GT_FC', 'HG_FC', 'PO_FC', 'FU_FC'

I've been playing around with the 'str' function and various other ways to format and convert to string, but I'm always returning the same original value list. Can anyone suggest a way to accomplish what I'm looking for?

Thanks, Mike

0 投票
2 回答
501 浏览

python - Unicode - 只打印字符串

如何只打印“打印我”。我不想打印其他块。

以上是 Python 调用时 shell 脚本的fabric输出run。我将fabric消息返回给我Flask的身份

更新

来自的回应flask如下:

我正在考虑删除\u001b[H2J和其他垃圾

0 投票
1 回答
611 浏览

python - 当 stdin 包含 unicode 时,Popen 子进程不退出

我正在执行一个子进程Popen,并按如下方式提供输入(使用 Python 2.7.4):

将条目添加到执行它的环境中是必要的,因为输入字符串包含日文字符,并且当脚本不是从命令行执行时(在我的情况下由 Apache 调用),Python 无法猜测编码。

这个设置对我使用其他命令效果很好,但是现在我正在使用chasen(一个日语标记器),每当我向它发送 unicode 字符时,子进程都不会返回,它只是坐在那里,Python 脚本会占用内存。这似乎是一个编码问题,但我认为我可以通过使用LC_ALL环境变量指定编码来解决这个问题。

编辑:额外的怪异如下......从命令行执行Python脚本时我没有遇到这个问题,除了'。'字符的显着例外。由于某种原因,这chasen也导致了奇怪。

0 投票
3 回答
873 浏览

python - 函数内的 Python 2.7 Unicode 错误(使用 __future__ print_function 和 unicode_literals)

我现在已经阅读了一些关于 unicode 的主题。

我正在使用 Python 2.7.2,但使用了未来的 print_function(因为原始的 print 语句对我来说很混乱......)

所以这里有一些代码:

现在如果我打印类似的东西

它完美地工作。但是,是的,我对 python 完全陌生,如果我声明一个将打印 unicode 字符串的函数,它会破坏我的脚本

一般来说,处理此错误和 unicode 的最佳方法是什么?我应该坚持使用 2.7 print 语句吗?

0 投票
1 回答
44 浏览

python - Copy element with management function

I to try run this code:

#xA;

But i have exception:

#xA;

I googled this problem and find this solution:

#xA;

The result was upset: All Russian symbols have been removed.

#xA;

How shall I copy the data correctly?

0 投票
1 回答
1707 浏览

unicode - Python:将ascii字符串转换为unicode字符串

我有一个 ascii 字符串,例如

"\u005c\u005c192.150.4.89\u005ctpa_test_python\u005c5.1\u005c\videoquality\u005crel_5.1.1Mx86\u005cblacklevelsetting\u005c\u5e8f\u5217\u5e8f\u5217.xml"

我想把它转换成 unicode 并转储到一个文件中,这样它就会像这样被转储:

"\\192.150.4.89\tpa\tpa_test_python\5.1\videoquality\logs\blacklevelsetting\序列序列.xml"

请分享你的想法。

谢谢, 阿布舍克