python阿拉伯编码问题 [英] python arabic encoding issue

查看:63
本文介绍了python阿拉伯编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Windows-1256编码的文本.现在我想将文本从阿拉伯语(windows-1256)转换为utf-8

i have a text with windows-1256 encoding. now i want to convert text from arabic(windows-1256) to utf-8

示例文本:

Óæí Ïæã ÈíåÞí

结果:

سوي دوم بيهقي

我使用此代码解码并编码为utf-8

i use this code to decode and encod to utf-8

# -*- coding: utf-8 -*-

data = "Óæí Ïæã ÈíåÞí"
print data.decode("windows-1256", "replace")
print data.encode("windows-1256")

该代码返回以下结果:

أ"أ¦أ­ أڈأ¦أ£ أˆأ­أ¥أ‍أ­
Traceback (most recent call last):
  File "mohmal2.py", line 5, in <module>
    print data.encode("windows-1256")
  File "/usr/lib/python2.7/encodings/cp1256.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

我找到了可以转换此文本的网站:

i found a site that can convert this text:

推荐答案

您似乎不小心将输入解码为Windows-1252.

It looks like you have accidentally decoded the input as Windows-1252.

>>> "Óæí Ïæã ÈíåÞí".encode('cp1252').decode('cp1256')
'سوي دوم بيهقي'

这篇关于python阿拉伯编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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