使用Python将六种文字转换为阿拉伯文字 [英] Convert from hexa to Arabic text with Python

查看:130
本文介绍了使用Python将六种文字转换为阿拉伯文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个将十六进制字符串转换为Unicode格式的代码,但是在打印后,当从输出进行复制并将其放入print(u'output')时,输出的转换无法正常工作

I build a code that convert a hexadecimal string into Unicode format but after print, the output the conversion not work while when making copy from output and put it in print(u'output') the Arabic text appear

Python代码

input ="062A06450020062A62C062F064A062F0020";
i = 0 ;
n ="\\"+"u";

    while i < (len(input)):
        n +=   input[i:i+4] + "\\"+"u";
        i = i + 4;

output = str(n[0:(len(n)-2)]) ;
print (u'%s'%output)

输出:

\u062A\u0645\u0020\u062A\u62C0\u62F0\u64A0\u62F0\u020

复制输出并使用打印Unicode:

Copy output and use print Unicode:

print (u'\u062A\u0645\u0020\u062A\u62C0\u62F0\u64A0\u62F0\u020')

出现阿拉伯文字

推荐答案

您不能通过在字符串 values 中添加\u来生成Unicode代码点,因为不是,因为\u序列是其中的一部分 literal 语法的名称. Python解析器(而不是解释器)使用它来生成Unicode值.

You can't produce Unicode codepoints by prepending \u in string values, no, because the \u sequence is part of the string literal syntax. It is used by the Python parser, no the interpreter, to produce Unicode values.

您的输入也太短;您可能需要在某个地方再输入一个数字 ,看起来您似乎在中间的62C之前缺少了中间的0.

Your input is also too short; you'd need one more digit somewhere, it looks like you are missing a 0 in the middle before the presumably in the middle before 62C.

您本质上具有按大端顺序排列的十六进制UTF-16;只需从十六进制解码并解码为utf-16-be:

You essentially have hexadecimal UTF-16 in big-endian order; just decode from hex and decode as utf-16-be:

from binascii import unhexlify
unhexlify(input).decode('utf-16-be')

演示,具有更正的输入数据:

Demo, with corrected input data:

>>> from binascii import unhexlify
>>> input ="062A06450020062A062C062F064A062F0020"
>>> unhexlify(input).decode('utf-16-be')
'تم تجديد '
>>> print(unhexlify(input).decode('utf-16-be'))
تم تجديد

这篇关于使用Python将六种文字转换为阿拉伯文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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