如何在python3中更改byte对象的编码?

2024-06-16 14:10:35 发布

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

我写了一个程序来抓取网页,得到一个json字幕。那是波斯语。我使用解码(“utf-8”),但我的字符是代码。 我该怎么办?你知道吗

我的python是3.4,操作系统是windows8,这是我的代码:

>>> import urllib.request as urllib2
>>> print(urllib2.urlopen('http://www.ted.com/talks/subtitles/id/667/lang/fa').read().decode("utf-8"))

{"captions":[{"duration":4000,"content":"\u0627\u0645\u0631\u0648\u0632\u0647 \u062a\u0645\u0627\u0645 \u0628\u0646\u0627\u0647\u0627 \u06cc\u06a9 \u0686\u06cc\u0632 \u0645\u0634\u062a\u0631\u06a9 \u062f\u0627\u0631\u0646\u062f.","startOfParagraph"...

第一行是: enter image description here

我使用这种方式将字符串写入文件,但问题仍然存在:

with open('D:\\result.json', 'w') as fid:
    fid.write(urllib2.urlopen('http://www.ted.com/talks/subtitles/id/667/lang/fa').read().decode("utf-8"))

Tags: 代码comjsonhttpaswwwurllib2utf
1条回答
网友
1楼 · 发布于 2024-06-16 14:10:35

这里有JSON,阿拉伯语字符转义为permitted by RFC 7159。您需要用^{}解析它才能撤消转义。完成之后,您应该能够提取“contents”值并将其打印(到一个文件中,因为控制台上有Windowscan't always display Unicode properly)。像这样:

>>> import urllib.request as urllib2
>>> result = json.loads(urllib2.urlopen('...').read().decode('utf8'))
>>> with open('example.txt', 'w', encoding='utf8') as f:
...     print(result['captions'][0]['content'], file=f)

你应该可以打开示例.txt你选择的编辑。如果显示不正确,请确保将编码设置为UTF-8。你知道吗

相关问题 更多 >