解析字典中的文本

我有一本包含以下文字的字典:

{
        "body": "Customer: \"I have a big problem. You cut off my head!\"\r\n\r\nme: \"I'm sorry? How did I cut off your head?\"\r\n\r\n(The customer shows me an obviously self-taken picture,with the top of his head removed.)\r\n\r\nme: \"Sir,it looks like it was taken that way.\"\r\n\r\nCustomer: \"No it wasn't! My whole head was there when I took it. I'm sure!\"\r\n\r\nme: \"Okay,let me see your memory card...\"\r\n\r\n(The customer hands it to me,and I go in the lab and pull it up on the computer. Sure enough,he chopped his own head off in the picture.)\r\n\r\nme: \"Sir,that is the whole image,and the top of your head isn't in it.\"\r\n\r\nCustomer: \"But it's DIGITAL,can't you fix it?\"\r\n\r\nme: \"You can't create something from nothing.\"\r\n\r\nCustomer: \"But... but... but... I need a photo for a dating website!\"\r\n\r\nme: \"Give me the camera and go stand over there.\"\r\n\r\nCustomer: *excited* \"Hot d***! You can be my best man!\"\r\n\r\nme: \"A thank you card will be enough.\"\r\n\r\n(Skip ahead 9 months...)\r\n\r\nFemale customer: \"Is your name ***?\"\r\n\r\nme: \"Yes,can I help you?\"\r\n\r\nFemale customer: \"My husband wanted you to have this.\" *hands me an envelope*\r\n\r\n(I open the envelope,and sure enough there's a thank you card with a picture of him and his wife. He actually got married and sent her in with the card!)","category": "Men / Women","id": 18189,"title": "A Heady Proposition"
    },

但是我完全不确定如何解析body的文本,以便可以从上面获取可读的文本。

我正在寻找一种通用的解决方案,而不是根据CustomerMe之类的关键字进行解析

文本应如下图所示:

解析字典中的文本

gouihk7 回答:解析字典中的文本

您可以按定界符模式\r\n\r\n+

进行拆分

使用repprint模块: pprint(re.split('\r\n\r\n+',your_dict['base'])) 生成:

['Customer: "I have a big problem. You cut off my head!"','Me: "I\'m sorry? How did I cut off your head?"','(The customer shows me an obviously self-taken picture,with the top of his '                                          
 'head removed.)','Me: "Sir,it looks like it was taken that way."','Customer: "No it wasn\'t! My whole head was there when I took it. I\'m '                                               
 'sure!"','Me: "Okay,let me see your memory card..."','(The customer hands it to me,and I go in the lab and pull it up on the '                                              
 'computer. Sure enough,he chopped his own head off in the picture.)',that is the whole image,and the top of your head isn\'t in it."','Customer: "But it\'s DIGITAL,can\'t you fix it?"','Me: "You can\'t create something from nothing."','Customer: "But... but... but... I need a photo for a dating website!"','Me: "Give me the camera and go stand over there."','Customer: *excited* "Hot d***! You can be my best man!"','Me: "A thank you card will be enough."','(Skip ahead 9 months...)','Female customer: "Is your name ***?"','Me: "Yes,can I help you?"','Female customer: "My husband wanted you to have this." *hands me an '                                                  
 'envelope*',"(I open the envelope,and sure enough there's a thank you card with a "                                                
 'picture of him and his wife. He actually got married and sent her in with '                                            
 'the card!)'] 

现在,您可以使用模块pyPdf将此行逐行打印为PDF,并选择一种漂亮的字体-实现+-之上的外观。

您需要添加更多代码以添加一些格式,例如上面的斜体;例如。: :之前的任何内容均应以粗体显示,而)(所在的任何行均应以斜体显示。

本文链接:https://www.f2er.com/2921897.html

大家都在问