- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我首先尝试使用 python 在 sqlite 中存储字符串,并得到消息:
sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
sqlite3.OperationalError: Could not decode to UTF-8 column 'tag_artist' with text 'Sigur Rós'
str.decode('source_encoding').encode('desired_encoding')
str.encode('desired_encoding')
\# sitecustomize.py
\# this file can be anywhere in your Python path,
\# but it usually goes in ${pythondir}/lib/site-packages/
import sys
sys.setdefaultencoding('utf_8')
test_str = "ó"
# -*- coding: utf_8 -*-
SyntaxError: Non-ASCII character '\xf3' in file _redacted_ on line 81, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
'�' = original char <type 'str'> repr(char)='\xf3'
'?' = unicode(char) ERROR: 'utf8' codec can't decode byte 0xf3 in position 0: unexpected end of data
'ó' = char.decode('latin_1') <type 'unicode'> repr(char.decode('latin_1'))=u'\xf3'
'?' = char.decode('utf_8') ERROR: 'utf8' codec can't decode byte 0xf3 in position 0: unexpected end of data
'ó' = original char <type 'str'> repr(char)='\xf3'
'ó' = unicode(char) <type 'unicode'> repr(unicode(char))=u'\xf3'
'ó' = char.decode('latin_1') <type 'unicode'> repr(char.decode('latin_1'))=u'\xf3'
'?' = char.decode('utf_8') ERROR: 'utf8' codec can't decode byte 0xf3 in position 0: unexpected end of data
'�' = original char <type 'str'> repr(char)='\xf3'
'�' = unicode(char) <type 'unicode'> repr(unicode(char))=u'\xf3'
'�' = char.decode('latin_1') <type 'unicode'> repr(char.decode('latin_1'))=u'\xf3'
'?' = char.decode('utf_8') ERROR: 'utf8' codec can't decode byte 0xf3 in position 0: unexpected end of data
#!/usr/bin/env python
# -*- coding: utf_8 -*-
import os
import sys
def encodingDemo(str):
validStrings = ()
try:
print "str =",str,"{0} repr(str) = {1}".format(type(str), repr(str))
validStrings += ((str,""),)
except UnicodeEncodeError as ude:
print "Couldn't print the str itself because the console is set to an encoding that doesn't understand some character in the string. See error:\n\t",
print ude
try:
x = unicode(str)
print "unicode(str) = ",x
validStrings+= ((x, " decoded into unicode by the default system encoding"),)
except UnicodeDecodeError as ude:
print "ERROR. unicode(str) couldn't decode the string because the system encoding is set to an encoding that doesn't understand some character in the string."
print "\tThe system encoding is set to {0}. See error:\n\t".format(sys.getdefaultencoding()),
print ude
except UnicodeEncodeError as uee:
print "ERROR. Couldn't print the unicode(str) because the console is set to an encoding that doesn't understand some character in the string. See error:\n\t",
print uee
try:
x = str.decode('latin_1')
print "str.decode('latin_1') =",x
validStrings+= ((x, " decoded with latin_1 into unicode"),)
try:
print "str.decode('latin_1').encode('utf_8') =",str.decode('latin_1').encode('utf_8')
validStrings+= ((x, " decoded with latin_1 into unicode and encoded into utf_8"),)
except UnicodeDecodeError as ude:
print "The string was decoded into unicode using the latin_1 encoding, but couldn't be encoded into utf_8. See error:\n\t",
print ude
except UnicodeDecodeError as ude:
print "Something didn't work, probably because the string wasn't latin_1 encoded. See error:\n\t",
print ude
except UnicodeEncodeError as uee:
print "ERROR. Couldn't print the str.decode('latin_1') because the console is set to an encoding that doesn't understand some character in the string. See error:\n\t",
print uee
try:
x = str.decode('utf_8')
print "str.decode('utf_8') =",x
validStrings+= ((x, " decoded with utf_8 into unicode"),)
try:
print "str.decode('utf_8').encode('latin_1') =",str.decode('utf_8').encode('latin_1')
except UnicodeDecodeError as ude:
print "str.decode('utf_8').encode('latin_1') didn't work. The string was decoded into unicode using the utf_8 encoding, but couldn't be encoded into latin_1. See error:\n\t",
validStrings+= ((x, " decoded with utf_8 into unicode and encoded into latin_1"),)
print ude
except UnicodeDecodeError as ude:
print "str.decode('utf_8') didn't work, probably because the string wasn't utf_8 encoded. See error:\n\t",
print ude
except UnicodeEncodeError as uee:
print "ERROR. Couldn't print the str.decode('utf_8') because the console is set to an encoding that doesn't understand some character in the string. See error:\n\t",uee
print
print "Printing information about each character in the original string."
for char in str:
try:
print "\t'" + char + "' = original char {0} repr(char)={1}".format(type(char), repr(char))
except UnicodeDecodeError as ude:
print "\t'?' = original char {0} repr(char)={1} ERROR PRINTING: {2}".format(type(char), repr(char), ude)
except UnicodeEncodeError as uee:
print "\t'?' = original char {0} repr(char)={1} ERROR PRINTING: {2}".format(type(char), repr(char), uee)
print uee
try:
x = unicode(char)
print "\t'" + x + "' = unicode(char) {1} repr(unicode(char))={2}".format(x, type(x), repr(x))
except UnicodeDecodeError as ude:
print "\t'?' = unicode(char) ERROR: {0}".format(ude)
except UnicodeEncodeError as uee:
print "\t'?' = unicode(char) {0} repr(char)={1} ERROR PRINTING: {2}".format(type(x), repr(x), uee)
try:
x = char.decode('latin_1')
print "\t'" + x + "' = char.decode('latin_1') {1} repr(char.decode('latin_1'))={2}".format(x, type(x), repr(x))
except UnicodeDecodeError as ude:
print "\t'?' = char.decode('latin_1') ERROR: {0}".format(ude)
except UnicodeEncodeError as uee:
print "\t'?' = char.decode('latin_1') {0} repr(char)={1} ERROR PRINTING: {2}".format(type(x), repr(x), uee)
try:
x = char.decode('utf_8')
print "\t'" + x + "' = char.decode('utf_8') {1} repr(char.decode('utf_8'))={2}".format(x, type(x), repr(x))
except UnicodeDecodeError as ude:
print "\t'?' = char.decode('utf_8') ERROR: {0}".format(ude)
except UnicodeEncodeError as uee:
print "\t'?' = char.decode('utf_8') {0} repr(char)={1} ERROR PRINTING: {2}".format(type(x), repr(x), uee)
print
x = 'ó'
encodingDemo(x)
最佳答案
I'm still ignorant of whether there is a way to correctly convert 'ó' from latin-1 to utf-8 and not mangle it
>>> oacute_latin1 = "\xF3"
>>> oacute_unicode = oacute_latin1.decode('latin1')
>>> oacute_utf8 = oacute_unicode.encode('utf8')
>>> print repr(oacute_latin1)
'\xf3'
>>> print repr(oacute_unicode)
u'\xf3'
>>> import unicodedata
>>> unicodedata.name(oacute_unicode)
'LATIN SMALL LETTER O WITH ACUTE'
>>> print repr(oacute_utf8)
'\xc3\xb3'
>>>
I switched to Unicode strings.
What gives? After reading this, describing exactly the same situation I'm in, it seems as if the advice is to ignore the other advice and use 8-bit bytestrings after all.
I didn't understand that the unicode characters still contained an implicit encoding. Am I saying that right?
It looks to me like unicode("\xF3") and "\xF3".decode('latin1') are the same when evaluated with repr().
>>> unicode("\xF3")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf3 in position 0: ordinal
not in range(128)
>>> "\xF3".decode('latin1')
u'\xf3'
>>>
u'\xf3' == '\xF3'.decode('latin1')
……这当然是真的。
unicode(str_object, encoding)
也是如此与
str_object.decode(encoding)
相同...包括在提供不适当的编码时爆炸。
Is that a happy circumstance
or is unicode("str") going to always return the correct decoding?
If not, when I receive a python str that has any possible character set in it, how do I know how to decode it?
print repr(your_data[:400])
...以及您拥有的有关其出处的任何抵押信息。
One more thing: there are apparently characters that Windows uses as Unicode for certain characters that aren't the correct Unicode for that character, so you may have to map those characters to the correct ones if you want to use them in other programs that are expecting those characters in the right spot.
Some applications add CP1252 (Windows, Western Europe) characters to documents marked up as ISO 8859-1 (Latin 1) or other encodings. These characters are not valid ISO-8859-1 characters, and may cause all sorts of problems in processing and display applications.
关于python - SQLite、python、unicode 和非 utf 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2392732/
UTF-8、UTF-16 和 UTF-32 之间有何区别? 据我所知,它们都将存储 Unicode,并且每个都使用不同数量的字节来表示字符。选择其中之一是否有优势? 最佳答案 当 ASCII 字符代表
好的。我知道这看起来像典型的“他为什么不直接用谷歌搜索或去 www.unicode.org 查一下?”问题,但对于这样一个简单的问题,在检查了两个来源后,我仍然无法回答。 我很确定这三种编码系统都支持
是否存在可以用 UTF-16 编码但不能用 UTF-8 编码的字符 最佳答案 没有。 UTF-* 是可以对全范围 Unicode 字符进行编码的编码。 编码之间的差异在于每个字符使用多少字节。 关于u
是否存在可以用 UTF-16 编码但不能用 UTF-8 编码的字符 最佳答案 没有。 UTF-* 是可以对全范围 Unicode 字符进行编码的编码。 编码之间的差异在于每个字符使用多少字节。 关于u
UTF-16 是一种双字节字符编码。交换两个字节的地址将产生 UTF-16BE 和 UTF-16LE。 但我发现在 Ubuntu gedit 文本编辑器中存在名称 UTF-16 编码,以及 UTF-1
我想将 UTF-16 字符串转换为 UTF-8。我通过 Unicode 发现了 ICU 库。我在转换时遇到问题,因为默认设置是 UTF-16。我试过使用转换器: UErrorCode myError
UTF-16 需要 2 个字节,UTF-8 需要 1 个字节。 而USB是面向8bit的,UTF-8更自然。 UTF-8 向后兼容 ASCII,而 UTF-16 则不然。 UTF-16 需要 2 个字
我对将 unicode 字符转换为十六进制值有点困惑。 我正在使用这个网站获取字符的十六进制值。 ( https://www.branah.com/unicode-converter ) 如果我输入“
我已经用UTF-8编码创建了一个文件,但是我不了解其在磁盘上占用的大小的规则。这是我的完整研究: 首先,我创建了一个带有印地语字母“'”的文件,Windows 7上的文件大小为 8个字节。 现在带有两
如何将WideString(或其他长字符串)转换为UTF-8中的字节数组? 最佳答案 这样的功能将满足您的需求: function UTF8Bytes(const s: UTF8String): TB
我有一个奇怪的验证程序,用于验证utf-8字符串是否是有效的主机名(PHP中的Zend Framework主机名valdiator)。它允许IDN(国际化域名)。它将比较每个子域与由其十六进制字节表示
在 utf16 和 utf32 中,一个字节的零是否意味着空?就像在 utf8 中一样,还是我们需要 2 个和 4 个字节的零来相应地在 utf16 和 utf32 中创建 null? 最佳答案 在
这是基于我的观察,对于 mysql,默认字符集 utf8 有点误导,它不支持完整的 Unicode,因为它无法存储四字节 UTF-8 编码的字符。它实际上是 utf8mb4 字符集,它是完整的 Uni
我只有处理 ASCII(单字节字符)的经验,并且阅读了很多关于人们如何以不同方式处理 Unicode 的帖子,这些帖子提出了他们自己的一系列问题。 此时我对 Unicode 的了解非常有限,我读到过U
我明白 std::codecvt在 C++11 中执行 UTF-16 和 UTF-8 之间的转换,并且 std::codecvt执行 UTF-32 和 UTF-8 之间的转换。是否可以在 UTF-8
我正在编写一个 HTTP 服务器并使用 trivial-utf-8:write-utf-8-bytes 来响应请求。我听说Babel就像trivial-utf-8但效率更高,所以我想试一试。搜索了一段
我正在设计一个新的 CMS,但想要设计它来满足我 future 的所有需求,比如多语言内容,所以我认为 Unicode (UTF-8) 是最好的解决方案 但是通过一些搜索我得到了这篇文章 http:/
例如,假设我在字符串中有以下 xml: 如果我尝试将其插入到带有 Xml 列的 SQL Server 2005 数据库表中,我将收到以下错误(我使用的是 EF 4.1,但我认为这无关紧要): XM
我正在使用 Python CSV 库读取两个 CSV 文件。 一种使用 UTF-8-BOM 编码,另一种使用 UTF-8 编码。在我的实践中,我发现使用“utf-8-sig”作为编码类型可以读取这两个
假设我的数据库设置如下以使用 utf-8(mysql 中的完整 4mb 版本) mysql_query("SET CHARACTER SET utf8mb4"); mysql_query("SET N
我是一名优秀的程序员,十分优秀!