在我正在尝试输出俄语字符的控制台它给了我??????????????
谁知道为什么?
我尝试写入文件 - 在这种情况下相同的情况.
例如
f=open('tets.txt','w')
f.write('some russian text')
f.close
Run Code Online (Sandbox Code Playgroud)
里面的文件是 - ??????????????????????
要么
p="some russian text"
print p
?????????????
Run Code Online (Sandbox Code Playgroud)
在额外的记事本中,不允许我用俄文字母保存文件.我这样说:
此文件包含Unicode格式的字符,如果将此文件另存为ANSI编码的文本文件,则会丢失该字符.要保留Unicode信息,请单击下面的"取消",然后从"编码"下拉列表中选择一个Unicode选项.继续?
如何调整我的系统,所以我不会有这个问题.
我已经使用从web服务检索的unicode字符串requests模块,它包含一个二进制文件的字节(PCL,因为它发生).其中一个字节的值为248,尝试对其进行base64编码会导致以下错误:
In [68]: base64.b64encode(response_dict['content']+'\n')
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
C:\...\<ipython-input-68-8c1f1913eb52> in <module>()
----> 1 base64.b64encode(response_dict['content']+'\n')
C:\Python27\Lib\base64.pyc in b64encode(s, altchars)
51 """
52 # Strip off the trailing newline
---> 53 encoded = binascii.b2a_base64(s)[:-1]
54 if altchars is not None:
55 return _translate(encoded, {'+': altchars[0], '/': altchars[1]})
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf8' in position 272: ordinal not in range(128)
In [69]: response_dict['content'].encode('base64')
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
C:\...\<ipython-input-69-7fd349f35f04> in <module>() …Run Code Online (Sandbox Code Playgroud) python base64 character-encoding unicode-string python-unicode
我有这个问题尝试使用lxml获取HTML文档中的所有文本节点,但我得到一个UnicodeEncodeError : 'ascii' codec can't encode character u'\xe9' in position 8995: ordinal not in range(128). 但是,当我试图找出这个页面的编码类型(encoding = chardet.detect(response)['encoding'])时,它说它是utf-8.单个页面有utf-8和ascii似乎很奇怪.实际上,这个:
fromstring(response).text_content().encode('ascii', 'replace')
Run Code Online (Sandbox Code Playgroud)
解决了这个问题.
这是我的代码:
from lxml.html import fromstring
import urllib2
import chardet
request = urllib2.Request(my_url)
request.add_header('User-Agent',
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)')
request.add_header("Accept-Language", "en-us")
response = urllib2.urlopen(request).read()
print encoding
print fromstring(response).text_content()
Run Code Online (Sandbox Code Playgroud)
输出:
utf-8
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 8995: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?请记住,我想用其他几个页面来做这个,所以我不想单独编码.
更新:
也许还有其他事情在这里发生.当我在终端上运行这个脚本时,我得到一个正确的输出但是当它在SublimeText中运行时,我得到UnicodeEncodeError ...¿?
UPDATE2:
当我使用此输出创建文件时,也会发生这种情况..encode('ascii', 'replace')工作,但我想有一个更通用的解决方案. …
我想知道为什么当我做:
a = [u'k',u'?',u'?']
Run Code Online (Sandbox Code Playgroud)
然后键入:
'k' in a
Run Code Online (Sandbox Code Playgroud)
我得到了True,同时:
'?' in a
Run Code Online (Sandbox Code Playgroud)
会给我False吗?
这真的让我很头疼,似乎有人故意让这个让人发疯...
我在Python IDE中启用了兼容性检查,现在我意识到继承的Python 2.7代码有很多调用unicode(),Python 3.x中不允许这样做.
我查看了Python2 的文档,发现没有提示如何升级:
我现在不想切换到Python3,但可能在将来.
该代码包含大约500个调用 unicode()
如何进行?
更新
用户vaultah阅读pyporting指南的评论 收到了几个赞成票.
我目前的解决方案就是这个(感谢Peter Brittain):
from builtins import str
Run Code Online (Sandbox Code Playgroud)
...我在pyporting docs中找不到这个暗示.....
我使用的是Ubuntu 12.04 LTS.当我在终端尝试这样的事情时:
rfx@digest:/usr/share/fonts/truetype/ttf-dejavu$ echo ??????????
??????????
Run Code Online (Sandbox Code Playgroud)
符号显示正确.但是如果尝试使用python 2.7打印unicode符号,我会得到:
>>> print u'???'
??????
Run Code Online (Sandbox Code Playgroud)
正如python显示我默认为终端使用utf-8编码:
>>> sys.stdout.encoding
'UTF-8'
Run Code Online (Sandbox Code Playgroud) 使用以下代码
lst = [u'\u5de5', u'\u5de5']
msg = repr(lst).decode('unicode-escape')
print msg
Run Code Online (Sandbox Code Playgroud)
我有
[u'?', u'?']
Run Code Online (Sandbox Code Playgroud)
如何删除前导u以使内容msg为:
['?', '?']
Run Code Online (Sandbox Code Playgroud) 我正在尝试用Unicode字符写出一个csv文件,所以我使用的是unicodecsv包.不幸的是,我仍然得到UnicodeDecodeErrors:
# -*- coding: utf-8 -*-
import codecs
import unicodecsv
raw_contents = 'He observes an “Oversized Gorilla” near Ashford'
encoded_contents = unicode(raw_contents, errors='replace')
with codecs.open('test.csv', 'w', 'UTF-8') as f:
w = unicodecsv.writer(f, encoding='UTF-8')
w.writerow(["1", encoded_contents])
Run Code Online (Sandbox Code Playgroud)
这是追溯:
Traceback (most recent call last):
File "unicode_test.py", line 11, in <module>
w.writerow(["1", encoded_contents])
File "/Library/Python/2.7/site-packages/unicodecsv/__init__.py", line 83, in writerow
self.writer.writerow(_stringify_list(row, self.encoding, self.encoding_errors))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codecs.py", line 691, in write
return self.writer.write(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codecs.py", line 351, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec …Run Code Online (Sandbox Code Playgroud) 我需要Python2中的正则表达式才能匹配水平白色空格而不是换行符.
\ s匹配包括换行符在内的所有空格.
>>> re.sub(r"\s", "", "line 1.\nline 2\n")
'line1.line2'
Run Code Online (Sandbox Code Playgroud)
\ h根本不起作用.
>>> re.sub(r"\h", "", "line 1.\nline 2\n")
'line 1.\nline 2\n'
Run Code Online (Sandbox Code Playgroud)
[\ t]有效,但我不确定我是否遗漏了其他可能的空白字符,特别是在Unicode中.如\ u00A0(非破空间)或\ u200A(发空间).以下链接中有更多空白字符.https://www.cs.tut.fi/~jkorpela/chars/spaces.html
>>> re.sub(r"[\t ]", "", u"line 1.\nline 2\n\u00A0\u200A\n", flags=re.UNICODE)
u'line1.\nline2\n\xa0\u200a\n'
Run Code Online (Sandbox Code Playgroud)
你有什么建议吗?
为什么我们在Python 3中有不同的面向字节的字符串表示?单个表示而不是多个表示是否足够?
对于ASCII范围编号打印,字符串显示以下列开头的序列\x:
In [56]: chr(128)
Out[56]: '\x80'
Run Code Online (Sandbox Code Playgroud)
在不同的数字范围内,Python使用以.开头的序列 \u
In [57]: chr(57344)
Out[57]: '\ue000'
Run Code Online (Sandbox Code Playgroud)
但是在最高范围内的数字,即截至目前的最大Unicode数,它使用了一个领先的\U:
In [58]: chr(1114111)
Out[58]: '\U0010ffff'
Run Code Online (Sandbox Code Playgroud) python-unicode ×10
python ×9
unicode ×5
python-2.7 ×3
python-3.x ×2
base64 ×1
encoding ×1
python-2.x ×1
regex ×1
string ×1
ubuntu ×1
urlfetch ×1
windows ×1