我想检查一个字符串是否是ASCII格式.
我知道ord()
,但是当我尝试时ord('é')
,我有TypeError: ord() expected a character, but string of length 2 found
.我知道它是由我构建Python的方式引起的(如ord()
文档中所述).
还有其他方法可以检查吗?
我有一个路径变量编码问题并将其插入SQLite数据库.我尝试使用编码("utf-8")功能解决它,这没有帮助.然后我使用了unicode()函数,它给了我unicode类型.
print type(path) # <type 'unicode'>
path = path.replace("one", "two") # <type 'str'>
path = path.encode("utf-8") # <type 'str'> strange
path = unicode(path) # <type 'unicode'>
Run Code Online (Sandbox Code Playgroud)
最后我获得了unicode类型,但是当路径变量的类型为str时,我仍然存在相同的错误
sqlite3.ProgrammingError:除非使用可解释8位字节串的text_factory(如text_factory = str),否则不得使用8位字节串.强烈建议您只需将应用程序切换为Unicode字符串.
你能帮我解决这个错误并解释正确的用法encode("utf-8")
和unicode()
功能吗?我经常和它搏斗.
编辑:
这个execute()语句引发了错误:
cur.execute("update docs set path = :fullFilePath where path = :path", locals())
Run Code Online (Sandbox Code Playgroud)
我忘了改变遇到同样问题的fullFilePath变量的编码,但我现在很困惑.我应该只使用unicode()或编码("utf-8")还是两者都使用?
我不能用
fullFilePath = unicode(fullFilePath.encode("utf-8"))
Run Code Online (Sandbox Code Playgroud)
因为它引发了这个错误:
UnicodeDecodeError:'ascii'编解码器无法解码位置32中的字节0xc5:序数不在范围内(128)
Python …
为什么这不起作用的任何想法?我真的认为'忽略'会做正确的事.
>>> 'add \x93Monitoring\x93 to list '.encode('latin-1','ignore')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0x93 in position 4: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud) 如果我有一个像这样的对象:
d = {'a':1, 'en': 'hello'}
Run Code Online (Sandbox Code Playgroud)
...然后我可以把它传递给urllib.urlencode
,没问题:
percent_escaped = urlencode(d)
print percent_escaped
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试传递一个值为type的对象unicode
,游戏结束:
d2 = {'a':1, 'en': 'hello', 'pt': u'olá'}
percent_escaped = urlencode(d2)
print percent_escaped # This fails with a UnicodeEncodingError
Run Code Online (Sandbox Code Playgroud)
所以我的问题是关于准备传递给对象的可靠方法urlencode
.
我想出了这个函数,我只是遍历对象并编码string或unicode类型的值:
def encode_object(object):
for k,v in object.items():
if type(v) in (str, unicode):
object[k] = v.encode('utf-8')
return object
Run Code Online (Sandbox Code Playgroud)
这似乎有效:
d2 = {'a':1, 'en': 'hello', 'pt': u'olá'}
percent_escaped = urlencode(encode_object(d2))
print percent_escaped
Run Code Online (Sandbox Code Playgroud)
那个输出a=1&en=hello&pt=%C3%B3la
,准备好传递给POST电话或其他什么.
但我的encode_object
功能对我来说真的很不稳定.首先,它不处理嵌套对象.
另一方面,如果声明,我会很紧张.我还应该考虑其他任何类型吗?
并且正在将这些type()
东西与本地对象进行比较,就像这个好习惯一样?
type(v) in …
Run Code Online (Sandbox Code Playgroud) 这是在python 2.4中.这是我的情况.我从一个数据库中提取一个字符串,它包含一个umlauted'o'(\ xf6).此时,如果我运行type(value),则返回str.然后我尝试运行.decode('utf-8'),我得到一个错误('utf8'编解码器无法解码1-4位的字节).
真的,我的目标就是成功使类型(值)返回unicode.我发现一个早期的问题 有一些有用的信息,但是所选答案中的例子似乎并不适用于我.我在这里做错了吗?
这是一些重现的代码:
Name = 'w\xc3\xb6rner'.decode('utf-8')
file.write('Name: %s - %s\n' %(Name, type(Name)))
Run Code Online (Sandbox Code Playgroud)
我从来没有真正进入write语句,因为它在第一个语句中失败了.
谢谢您的帮助.
编辑:
我验证了DB的字符集是utf8.因此,在我的代码重现中,我将'\ xf6'更改为'\ xc3\xb6',但仍然会发生故障.'utf-8'和'utf8'之间有区别吗?
使用编解码器写入文件的提示很方便(我肯定会使用它),但在这种情况下,我只是为了调试目的而写入日志文件.
在处理unicode问题时,我发现unicode(self)
并且self.__unicode__()
有不同的行为:
#-*- coding:utf-8 -*-
import sys
import dis
class test():
def __unicode__(self):
s = u'??'
return s.encode('utf-8')
def __str__(self):
return self.__unicode__()
print dis.dis(test)
a = test()
print a
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常,但如果我self.__unicode__()
改为unicode(self)
,它将显示错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
有问题的代码是:
#-*- coding:utf-8 -*-
import sys
import dis
class test():
def __unicode__(self):
s = u'??'
return s.encode('utf-8')
def __str__(self):
return unicode(self)
print dis.dis(test)
a = test()
print a
Run Code Online (Sandbox Code Playgroud)
非常好奇python如何处理这个,我试过dis模块,但没有看到太多的区别: …