简体   繁体   English

python如何将ascii代码转换为原始字符

[英]python how to convert ascii codes to original characters

Using python 2.7.2 使用python 2.7.2

I'm having issues with special characters with ascii values > 128 我遇到了ascii值> 128的特殊字符问题

The problem is that our data got corrupted by iso vs utf8 convertion. 问题是我们的数据被iso vs utf8转换破坏了。 so We are trying to fix this. 所以我们正试图解决这个问题。

Where are trying to fix it using python. 在哪里尝试使用python修复它。 We loop through our data character by character fix the corrupted values. 我们通过字符循环我们的数据字符来修复损坏的值。 and get an ascii # code. 并得到一个ascii#代码。 let say I want to replace ascii code 226 with ascii code 146, which is a special quote "'": 假设我想用ascii代码146替换ascii代码226,这是一个特殊的引号“'”:

c='â'
   print ord ( c ) 
   226 

How can I decode 226 back to â or convert ascii 146 to "'"? 如何将226解码回â或将ascii 146转换为“'”?

Instead of the chr function you will want the unichr function: 而不是chr函数,你将需要unichr函数:

 >>> c = u'â'
 >>> print(ord(c))
 226
 >>> unichr(226)
 u'\xe2'
 >>> print(unichr(226))
 â

See https://docs.python.org/2/howto/unicode.html 请参阅https://docs.python.org/2/howto/unicode.html

You probably want the chr function. 你可能想要chr功能。 Or is your problem that you don't have the right codepage? 或者您的问题是您没有正确的代码页?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM