在python中解码字符串

2024-09-28 01:33:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的绳子:

u"examined by other children\u2014particularly the children of George MacDonald."

但我找不到破解它的方法。python需要什么编码器/解码器。还有许多“\n \nOn”行距,我要删除它们。我试过了字符串.解码(“utf-8”)

TypeError: 'unicode' object is not callable

编辑:

这是维基百科《爱丽丝梦游仙境》中较大的一段:

u'BackgroundAlice was published in 1865, three years after the Reverend Charles Lutwidge Dodgson and the Reverend Robinson Duckworth rowed in a boat, on 4 July 1862, [4] up the Isis with the three young daughters of Henry Liddell (the Vice-Chancellor of Oxford University and Dean of Christ Church): Lorina Charlotte Liddell (aged 13, born 1849) ("Prima" in the book\'s prefatory verse); Alice Pleasance Liddell (aged 10, born 1852) ("Secunda" in the prefatory verse); Edith Mary Liddell (aged 8, born 1853) ("Tertia" in the prefatory verse). [5]\n\nThe journey began at Folly Bridge near Oxford and ended five miles away in the village of Godstow. During the trip the Reverend Dodgson told the girls a story that featured a bored little girl named Alice who goes looking for an adventure. The girls loved it, and Alice Liddell asked Dodgson to write it down for her. He began writing the manuscript of the story the next day, although that earliest version no longer exists. The girls and Dodgson took another boat trip a month later when he elaborated the plot to the story of Alice, and in November he began working on the manuscript in earnest. [6]\n\nTo add the finishing touches he researched natural history for the animals presented in the book, and then had the book examined by other children\u2014particularly the children of George MacDonald. He added his own illustrations but approached John Tenniel to illustrate the book for publication, telling him that the story had been well liked by children. [6]\n\nOn 26 November 1864 he gave Alice the handwritten manuscript of Alice\'s Adventures Under Ground, with illustrations by Dodgson himself, dedicating it as "A Christmas Gift to a Dear Child in Memory of a Summer\'s Day". [7] Some, including Martin Gardner, speculate there was an earlier version that was destroyed later by Dodgson when he wrote a more elaborate copy by hand. [8]\n\nBut before Alice received her copy, Dodgson was already preparing it for publication and expanding the 15,500-word original to 27,500 words, [9] most notably adding the episodes about the Cheshire Cat and the Mad Tea-Party.'

我想要一个可以被解析的纯文本。我得到了一个错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2014' in position 1295: ordinal not in range(128)

当我尝试的时候段落解码(“unicode_escape”)或段落“string_escape”)。在


Tags: andofthetoinforbyhe
1条回答
网友
1楼 · 发布于 2024-09-28 01:33:16

它已经是Unicode,因此字符串开头的^{{cd1>}。您只需要^{cd2>}字节字符串。^{cd3>}只是^{cd4>}的Unicode字符,您正在显示字符串的^{{cd5>}。^{cd6>}如果您是控制台支持的话,字符串很可能会看到^{cd4>}。打印控制台编码中的Unicode字符串自动进行编码。在这里,我已经将Windows控制台切换到支持em破折号的代码页(默认cp437不支持):

C:>chcp 1252
Active code page: 1252

C:\>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x=u"examined by other children\u2014particularly the children of George MacDonald."
>>> print x
examined by other children—particularly the children of George MacDonald.

相关问题 更多 >

    热门问题