相关疑难解决方法(0)

正确的方法来定义Python源代码编码

PEP 263定义了如何声明Python源代码编码.

通常,Python文件的前两行应该以:

#!/usr/bin/python
# -*- coding: <encoding name> -*-
Run Code Online (Sandbox Code Playgroud)

但我看到很多文件以:

#!/usr/bin/python
# -*- encoding: <encoding name> -*-
Run Code Online (Sandbox Code Playgroud)

=> 编码而不是编码.

那么声明文件编码的正确方法是什么?

是否允许编码,因为使用的正则表达式是懒惰的?或者它只是声明文件编码的另一种形式?

我问这个问题是因为PEP没有谈论编码,它只是谈论编码.

python encoding

156
推荐指数
4
解决办法
8万
查看次数

如何从文本中提取所有表情符号?

请考虑以下列表:

a_list = ['  me así, bla es se  ds ']
Run Code Online (Sandbox Code Playgroud)

如何在新列表中提取内部的所有表情符号a_list?:

new_lis = ['     ']
Run Code Online (Sandbox Code Playgroud)

我试图使用正则表达式,但我没有所有可能的表情符号编码.

python python-3.x emoji

40
推荐指数
6
解决办法
3万
查看次数

Python:在包含代理项对时获取正确的字符串长度

考虑以下IPython交流:

In [1]: s = u'??????'

In [2]: len(s)
Out[2]: 8
Run Code Online (Sandbox Code Playgroud)

应该是正确的输出7,但由于这七个中文字符中的第五个具有高Unicode代码点,因此它以UTF-8表示为"代理对",而不仅仅是一个简单的代码点,因此Python认为它是两个字符而不是一个字符.

即使我使用unicodedata,它将代理对正确地作为单个代码点(\U00026177)返回,当传递给len()错误的长度时仍然会返回:

In [3]: import unicodedata

In [4]: unicodedata.normalize('NFC', s)
Out[4]: u'\u83ef\u889e\u8207\u7dfc\U00026177\u540c\u6b78'


In [5]: len(unicodedata.normalize('NFC', s))
Out[5]: 8
Run Code Online (Sandbox Code Playgroud)

如果不采取像UTF-32重新编译Python这样的重大步骤,是否有一种简单的方法可以在这种情况下获得正确的长度?

我正在使用IPython 0.13,Python 2.7.2,Mac OS 10.8.2.

python surrogate-pairs

14
推荐指数
2
解决办法
4276
查看次数

将表情符号视为正则表达式中的一个字符

这是一个小例子:

reg = ur"((?P<initial>[+\-])(?P<rest>.+?))$"
Run Code Online (Sandbox Code Playgroud)

(在这两种情况下文件都有-*- coding: utf-8 -*-)

在Python 2中:

re.match(reg, u"hello").groupdict()
# => {u'initial': u'\ud83d', u'rest': u'\udc4dhello'}
# unicode why must you do this
Run Code Online (Sandbox Code Playgroud)

然而,在Python 3中:

re.match(reg, "hello").groupdict()
# => {'initial': '', 'rest': 'hello'}
Run Code Online (Sandbox Code Playgroud)

上述行为是100%完美,但切换到Python 3目前不是一个选项.将3的结果复制到2中的最佳方法是什么,这适用于窄版和宽版Python?似乎是以"\ ud83d\udc4d"格式来找我,这就是让这个变得棘手的原因.

python regex python-2.7 python-unicode unicode-literals

10
推荐指数
1
解决办法
622
查看次数

如何使用python在字符串中查找和计算表情符号?

已在link1,link2,link3处针对基于文本的表情符号解决了此主题.但是,我想做一些与匹配简单表情符号略有不同的东西.我正在整理包含表情符号图标的推文.以下unicode信息仅包含此类表情符号:pdf.

使用带有英文单词的字符串,其中也包含pdf中的任何这些表情符号,我希望能够将表情符号的数量与单词数量进行比较.

我向前走的方向似乎不是最佳选择,我正在寻求一些帮助.正如您在下面的脚本中看到的那样,我只是计划从命令行完成工作:

$cat <file containing the strings with emoticons> | ./emo.py
Run Code Online (Sandbox Code Playgroud)

emo.py伪造脚本:

import re
import sys

for row in sys.stdin:
    print row.decode('utf-8').encode("ascii","replace")
    #insert regex to find the emoticons
    if match:
       #do some counting using .split(" ")
       #print the counting
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是解码/编码.我还没有找到一个如何编码/解码字符串的好选项,所以我可以正确找到图标.我想要搜索以查找单词和表情符号数量的字符串示例如下:

"笑脸图释岩!在此输入图像描述 我喜欢你在此输入图像描述".

挑战:你能编写一个脚本来计算这个字符串中的单词和表情符号的数量吗?请注意,表情符号都位于单词旁边,两者之间没有空格.

python regex string unicode

9
推荐指数
2
解决办法
1万
查看次数

对于单个Unicode字符串,Python返回长度为2

在Python 2.7中:

In [2]: utf8_str = '\xf0\x9f\x91\x8d'
In [3]: print(utf8_str)

In [4]: unicode_str = utf8_str.decode('utf-8')
In [5]: print(unicode_str)
 
In [6]: unicode_str
Out[6]: u'\U0001f44d'
In [7]: len(unicode_str)
Out[7]: 2
Run Code Online (Sandbox Code Playgroud)

由于unicode_str只包含一个unicode代码点(0x0001f44d),为什么len(unicode_str)返回2而不是1?

python unicode python-2.7 python-unicode

7
推荐指数
1
解决办法
1302
查看次数