UTF-8拉丁-1转换问题,python django [英] UTF-8 latin-1 conversion issues, python django

查看:188
本文介绍了UTF-8拉丁-1转换问题,python django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定,所以我的问题是我有字符串'\222\222\223\225',它作为latin-1存储在db中。我从django(通过打印它)是以下字符串,'ââ¢',我假设是它的UTF转换。现在我需要将字符串传递到一个函数,
做这个操作:

ok so my issue is i have the string '\222\222\223\225' which is stored as latin-1 in the db. What I get from django (by printing it) is the following string, 'ââââ¢' which I assume is the UTF conversion of it. Now I need to pass the string into a function that does this operation:

strdecryptedPassword + chr(ord(c) - 3 - intCounter - 30)

我得到这个错误:


chr()arg不在范围内(256)

chr() arg not in range(256)

将字符串编码为latin-1首先我得到这个错误:

If I try to encode the string as latin-1 first I get this error:


'latin-1'编解码器不能编码位置0的字符-3:序数不是
在范围(256)

'latin-1' codec can't encode characters in position 0-3: ordinal not in range(256)

我已经读了一堆字符编码的工作原理,

I have read a bunch on how character encoding works, and there is something I am missing because I just don't get it!

推荐答案

您的第一个错误'chr()arg不在范围内(256)'可能意味着你有下溢的值,因为chr不能取负数。我不知道加密算法应该做什么时,inputcounter + 33比实际的字符表示,你将不得不检查在这种情况下做什么。

Your first error 'chr() arg not in range(256)' probably means you have underflowed the value, because chr cannot take negative numbers. I don't know what the encryption algorithm is supposed to do when the inputcounter + 33 is more than the actual character representation, you'll have to check what to do in that case.

关于第二个错误。您必须解码()而不是编码()一个常规字符串对象,以获得正确的数据表示。 encode()采用unicode对象(以u'开头的对象),并生成要输出或写入文件的常规字符串。 decode()接受一个字符串对象并生成一个具有相应代码点的unicode对象。这是通过unicode()调用从字符串对象生成时完成的,您也可以调用a.decode('latin-1')。

About the second error. you must decode() and not encode() a regular string object to get a proper representation of your data. encode() takes a unicode object (those starting with u') and generates a regular string to be output or written to a file. decode() takes a string object and generate a unicode object with the corresponding code points. This is done with the unicode() call when generated from a string object, you could also call a.decode('latin-1') instead.

>>> a = '\222\222\223\225'
>>> u = unicode(a,'latin-1')
>>> u
u'\x92\x92\x93\x95'
>>> print u.encode('utf-8')
ÂÂÂÂ
>>> print u.encode('utf-16')
ÿþ
>>> print u.encode('latin-1')

>>> for c in u:
...   print chr(ord(c) - 3 - 0 -30)
...
q
q
r
t
>>> for c in u:
...   print chr(ord(c) - 3 -200 -30)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ValueError: chr() arg not in range(256)

这篇关于UTF-8拉丁-1转换问题,python django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆