如何使用 Python 3 处理 utf-8 文本?

新手上路,请多包涵

我需要解析各种文本源,然后将其打印/存储在某处。

每次遇到非 ASCII 字符时,我都无法正确打印它,因为它被转换为字节,而且我不知道如何查看正确的字符。

(我是 Python 的新手,我来自 PHP,我从来没有遇到过任何 utf-8 问题)

下面是一个代码示例:

 #!/usr/bin/python
# -*- coding: utf-8 -*-

import codecs
import feedparser

url = "http://feeds.bbci.co.uk/japanese/rss.xml"
feeds = feedparser.parse(url)
title = feeds['feed'].get('title').encode('utf-8')

print(title)

file = codecs.open("test.txt", "w", "utf-8")
file.write(str(title))
file.close()

我想打印并在文件中写入 RSS 标题(BBC 日语 - ホーム),但结果是这样的:

b’BBC 日语 - \xe3\x83\x9b\xe3\x83\xbc\xe3\x83\xa0’

在屏幕和文件上。有没有正确的方法来做到这一点?

原文由 Omiod 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 566
2 个回答

In python3 bytes and str are two different types - and str is used to represent any type of string (also unicode), when you encode() 一些东西,你将它从它的 str 表示转换为它的 bytes 表示特定编码。

在您的情况下,为了解码字符串,您只需要删除 encode('utf-8') 部分:

 #!/usr/bin/python
# -*- coding: utf-8 -*-

import codecs
import feedparser

url = "http://feeds.bbci.co.uk/japanese/rss.xml"
feeds = feedparser.parse(url)
title = feeds['feed'].get('title')

print(title)

file = codecs.open("test.txt", "w", encoding="utf-8")
file.write(title)
file.close()

原文由 Dean Fenster 发布,翻译遵循 CC BY-SA 3.0 许可协议

JSON 数据转 Unicode 支持日文字符

def jsonFileCreation (messageData, fileName):
   with open(fileName, "w", encoding="utf-8") as outfile:
         json.dump(messageData, outfile, indent=8, sort_keys=False,ensure_ascii=False)

原文由 Tarkeshwar Prasad 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进