Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Python)如何将字节字符串还原为原始字符串?_Python_String_Encoding - Fatal编程技术网

(Python)如何将字节字符串还原为原始字符串?

(Python)如何将字节字符串还原为原始字符串?,python,string,encoding,Python,String,Encoding,我一直在解析网站,结果发现我以一种不太可取的方式将内容保存到json文件中,并且似乎无法对这个过程进行反向工程。基本上,我保存了BeautifulSoup标签,如下所示 r = requests.get(url).content soup = BeautifulSoup(r, "html.parser") article = soup.find("article") b = article.encode() c = str(b) # save c in json structure usin

我一直在解析网站,结果发现我以一种不太可取的方式将内容保存到json文件中,并且似乎无法对这个过程进行反向工程。基本上,我保存了BeautifulSoup标签,如下所示

r = requests.get(url).content
soup = BeautifulSoup(r, "html.parser")
article = soup.find("article")

b = article.encode()
c = str(b) 
# save c in json structure using json.dump(f)
我希望能够获得给定c的文章

使用编解码器似乎几乎能让我达到目的:

import codecs
codecs.getdecoder("unicode_escape")(c)[0]
然而斯堪的纳维亚字母å,ä,ö没有正确解码

简而言之:

输入:

'b\'<article> \\n L\\xc3\\xa4s bl.a. om Gasporox nya m\\xc3\\xa4tkoncept f\\xc3\\xb6r tr\\xc3\\xa5g, en intervju med styrelseledamoten Per Nystr\\xc3\\xb6m och nyheter fr\\xc3\\xa5n GPX Medical om bland annat projekten Sinuslight och Neo-Lung.\\n</article>''
'b\'\\n L\\xc3\\xa4s bl.a。om Gasporox nya m\\xc3\\xa4tconcept f\\xc3\\xb6r tr\\xc3\\xa5g,每个Nystr\\xc3\\xb6m och nyheter fr\\xc3\\xa5n GPX医疗om bland annat项目的鼻窦灯或新肺。\\n“
期望输出:

<article>
Läs bl.a. om Gasporox nya mätkoncept för tråg, en intervju med
styrelseledamoten Per Nyström och nyheter från GPX Medical om 
bland annat projekten Sinuslight och Neo-Lung.
</article> 

Läs bl.a。om Gasporox nya mätkoncept för tråg,中间医学
根据Nyström och nyheter från GPX医疗om的styrelseledamoten
平淡的安纳特项目——新肺。

提前谢谢

您需要评估
b''
值,然后使用
UTF-8

import ast
x = "b\'<article> \\n L\\xc3\\xa4s bl.a. om Gasporox nya m\\xc3\\xa4tkoncept f\\xc3\\xb6r tr\\xc3\\xa5g, en intervju med styrelseledamoten Per Nystr\\xc3\\xb6m och nyheter fr\\xc3\\xa5n GPX Medical om bland annat projekten Sinuslight och Neo-Lung.\\n</article>'"

x = ast.literal_eval(x)

result = x.decode("utf-8")
导入ast
x=“b\'\\n L\\xc3\\xa4s bl.a.om Gasporox nya m\\xc3\\xa4tconcept f\\xc3\\xb6r\\xc3\\xa5g,每Nystr\\xc3\\xb6m och nyheter fr\\xc3\\xa5n GPX医疗om温和的ANAT项目,用于新肺的鼻窦手术。\\n”
x=ast.literal\u eval(x)
结果=x.decode(“utf-8”)