简体   繁体   English

在Python中将Hex NCR文本表示形式转换为Unicode

[英]Converting Hex NCR text representations to Unicode in Python

I have a string parsed from a web page originally in chinese as: 我有一个从网页解析的字符串,该字符串最初是中文的:

若き葉末には風が立ち 森は翡翠の息を返す 雲の切れ間から注ぐ 光に君を見初めん

碧き瞳のほほえむとき そは鐘のひびき胸に打つ さしのべた腕に絡む 蔦の葉に君を逃す

残る 香り 水面をかけゆく恋の舟 つかの間の波に 揺られ

やさしき幻影 心に映るその姿よ 永遠なる君に 想いを捧げん

若き葉末には風は眠り 森は密やかに息を止む 抱きしめた腕のなかで 静かに君は消えゆく

月は 満ちて 黄金の羽根が舞いおちる 我はただひとり森に

祈りたまえや

However in the process of parsing it, it was converted into Hex NCR string in the following form: 但是,在解析过程中,它以以下形式转换为十六进制NCR字符串:

若き葉末には風が立ち\n森は翡翠の息を返す\n雲の切れ間から注ぐ\n光に君を見初めん\n\n碧き瞳のほほえむとき\nそは鐘のひびき胸に打つ\nさしのべた腕に絡む\n蔦の葉に君を逃す\n\n残る 香り\n水面をかけゆく恋の舟\nつかの間の波に 揺られ\n\nやさしき幻影 心に映るその姿よ\n永遠なる君に 想いを捧げん\n\n若き葉末には風は眠り\n森は密やかに息を止む\n抱きしめた腕のなかで\n静かに君は消えゆく\n\n月は 満ちて\n黄金の羽根が舞いおちる\n我はただひとり森に\n\n祈りたまえや

I want to convert this string into its appropriate unicode format. 我想将此字符串转换为适当的unicode格式。

From my research I have been able to gather that for example 一 从我的研究中,我能够收集到例如一 maps to the unicode string b'\\\一' . 映射到unicode字符串b'\\\一'

This can be manually done by stripping &#x and prefixing a \\\\u\u003c/code> to the beginning of the string along with making the whole thing lowercase and converting to a bytestring by adding a b before the string. 这可以通过以下方法手动完成:删除&#x并在字符串的开头加上\\\\u\u003c/code>前缀,然后使整个内容变为小写,并通过在字符串之前添加b转换为字节字符串。 This is done in this repo but through the use of the inefficient eval function through code such as eval("b'\\\一") . 此操作在此存储库中完成,但是通过使用诸如eval("b'\\\一")代码来使用效率低下的eval函数。

[EDIT: The above para is incorrect. [编辑:以上段落不正确。 It is not a bytestring but a unicode string as present in python2. 它不是python2中存在的字节串而是unicode字符串。 The correct mapping would be 一 正确的映射应为一 -> u'\一' ] -> u'\一' ]

Is there a better way to do this? 有一个更好的方法吗? Considering edge cases where these hex map strings can be present in the middle of regular text such as here: 考虑这些十六进制映射字符串可以出现在常规文本中间的边缘情况,例如:

Je me levais tôt
Travailler en homme
Je me souviens du goût
Du café brûlant
Dans la tasse rouge
Et la femme qui dort
Les portes ouvertes de la grande usine
Bouffaient nos fils le jour de leurs quinze ans
On se levait tôt
Sortis de nos draps
On se retrouvait en bas
Les rues du village s'allumaient d'un coup
A six heures moins le quart
Les portes ouvertes de la grande usine
Bouffaient nos fils bien avant leurs quinze ans
On se lève trop tôt
On sait plus quoi faire
Dans le café des vieux
Les mains dans nos poches
Cachent nos poings noirs
Y'a plus qu'à qui change pas
Les portes sont fermées
Y'a plus de feu qui gronde
L'usine a tout vomi d'un seul coup
Pourquoi on fait ça
Pourquoi ça m'fait ça
Pourquoi on nous fait ça à nous

I am dealing with a large set of data where such characters can be strewn anywhere, and I need a meaningful way to deal with them. 我正在处理大量数据,这些字符可以散布在任何地方,并且我需要一种有意义的方式来处理它们。

So is there any better way to do this? 那么,有什么更好的方法吗? Ideally one that is supported inherently by python. 理想情况下,python固有支持的一种。

If someone has a solution to my problem here, I will be immensely grateful. 如果有人可以解决我的问题,我将不胜感激。 Thanks in advance. 提前致谢。

Have a look at the html module in the standard library: 看一下标准库中的html模块:

>>> import html
>>> html.unescape('Je me levais tôt')
'Je me levais tôt'
>>> html.unescape('若き葉末には')
'若き葉末には'

The result is a Unicode string (type str in Python 3). 结果是一个Unicode字符串(Python 3中的类型str )。 Note that the b'...' notation is for byte strings. 请注意, b'...'表示字节字符串。 The literal b'\\\一' in your example does not make much sense, since it is a byte string with 6 characters (\\, u, 4, e, 0, 0). 在您的示例中,文字b'\\\一'没有多大意义,因为它是一个包含6个字符(\\,u,4,e,0、0)的字节串。 You probably meant '\一' (or u'\一' in Python 2), which is a single-character Unicode string. 您可能要使用'\一' (或Python 2中的u'\一' ),这是一个单字符Unicode字符串。

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

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