我写了一个程序来抓取网页,得到一个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"...
我使用这种方式将字符串写入文件,但问题仍然存在:
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"))
这里有JSON,阿拉伯语字符转义为permitted by RFC 7159。您需要用^{} 解析它才能撤消转义。完成之后,您应该能够提取“contents”值并将其打印(到一个文件中,因为控制台上有Windowscan't always display Unicode properly)。像这样:
你应该可以打开示例.txt你选择的编辑。如果显示不正确,请确保将编码设置为UTF-8。你知道吗
相关问题 更多 >
编程相关推荐