Python:将字符串从UTF-8转换为Latin-1 [英] Python: convert string from UTF-8 to Latin-1

查看:1471
本文介绍了Python:将字符串从UTF-8转换为Latin-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到堆叠在这里尝试用Python 2.5更改编码



我有XML响应,我编码为UTF-8:响应。编码( 'UTF-8')。这很好,但使用这个信息的程序不喜欢这个编码,我必须将其转换为其他代码页。实际的例子是,我使用ghostscript python模块将pdfmark数据嵌入PDF文件 - 最终结果是在Acrobat中出现错误的字符。



我已经做了大量的组合, code> .encode()和 .decode()在'utf-8'和'latin-1'之间,它驱使我疯狂,因为我无法输出正确的结果。



如果我输出一个文件到 .encode('utf-8')然后将该文件从UTF-8转换为CP1252(也称为拉丁1),即ie iconv.exe ,并嵌入数据一切正常。



基本上有人可以帮我转换,即以UTF-8编码的字符á为hex: C3 A1 to latin -1为十六进制: E1



提前感谢

解决方案

代替 .encode('utf-8'),使用 .encode('latin -1')


I feel stacked here trying to change encodings with Python 2.5

I have XML response, which I encode to UTF-8: response.encode('utf-8'). That is fine, but the program which uses this info doesn't like this encoding and I have to convert it to other code page. Real example is that I use ghostscript python module to embed pdfmark data to a PDF file - end result is with wrong characters in Acrobat.

I've done numerous combinations with .encode() and .decode() between 'utf-8' and 'latin-1' and it drives me crazy as I can't output correct result.

If I output the string to a file with .encode('utf-8') and then convert this file from UTF-8 to CP1252 (aka latin-1) with i.e. iconv.exe and embed the data everything is fine.

Basically can someone help me convert i.e. character á which is UTF-8 encoded as hex: C3 A1 to latin-1 as hex: E1?

Thanks in advance

解决方案

Instead of .encode('utf-8'), use .encode('latin-1').

这篇关于Python:将字符串从UTF-8转换为Latin-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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