【问题标题】:Getting readable text from a large json file in python从python中的大型json文件中获取可读文本
【发布时间】:2020-04-01 19:28:27
【问题描述】:

我有一个简单的问题。在 python 中,我得到了一个非常大的字符串,其中包含一些文本,例如: \u0398\u03b5\u03b1\u03c4\u03c1\u03b9\u03ba\u03cc

我需要的只是将其转换为可读的文本。我正在查看其他一些帖子,但没有找到任何解决方案。

编辑:尝试了以下方法,但没有成功:

rawtext = str(json.dumps(result, indent=2, sort_keys=True))
puretext = str(rawtext)
with open("result.txt", "a+", encoding="utf-8-sig") as f:
    f.write(puretext)

这是来自 result.txt 文件的示例:

    {
      "date": "\u03a0\u03c1\u03b9\u03bd \u03b1\u03c0\u03cc 3 \u03ce\u03c1\u03b5\u03c2",
      "date_utc": "2020-04-01T16:12:41.903Z",
      "domain": "www.protothema.gr",
      "link": "https://www.protothema.gr/culture/article/991328/menoume-spiti-tzaz-taxidia-apo-to-kedro-politismou-idruma-stauros-niarhos/",
      "position": 1,
      "snippet": "... \u03c4\u03bf \u0398\u03b5\u03b1\u03c4\u03c1\u03b9\u03ba\u03cc \u0391\u03bd\u03b1\u03bb\u03cc\u03b3\u03b9\u03bf \u03c4\u03bf\u03c5 \u039a\u03ad\u03bd\u03c4\u03c1\u03bf\u03c5 \u03a0\u03bf\u03bb\u03b9\u03c4\u03b9\u03c3\u03bc\u03bf\u03cd \u038a\u03b4\u03c1\u03c5\u03bc\u03b1 \u03a3\u03c4\u03b1\u03cd\u03c1\u03bf\u03c2 \u039d\u03b9\u03ac\u03c1\u03c7\u03bf\u03c2! \u03a3\u03c4o \u03c0\u03c1\u03ce\u03c4o \u03b5\u03b2\u03b4\u03bf\u03bc\u03b1\u03b4\u03b9\u03b1\u03af\u03bf \u03c4\u03b6\u03b1\u03b6 \u03c1\u03b1\u03bd\u03c4\u03b5\u03b2\u03bf\u03cd \u03bf \u03ba\u03bf\u03c1\u03c5\u03c6\u03b1\u03af\u03bf\u03c2 \u0388\u03bb\u03bb\u03b7\u03bd\u03b1\u03c2 \u03c0\u03b9\u03b1\u03bd\u03af\u03c3\u03c4\u03b1\u03c2 \u03c4\u03b7\u03c2 jazz,\u00a0...",
      "source": "\u03a0\u03c1\u03ce\u03c4\u03bf \u0398\u0395\u039c\u0391",
      "thumbnail": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSGRNpdq5fhYi7be2t7UZ-hh-cQvjJqtsnJhN0ShCL7A6DqqPH9aop33FRGTcyfF2gsaU09SG-P&s",
      "title": "\u00ab\u039c\u03b5\u03bd\u03bf\u03c5\u03bc\u03b5 \u03a3\u03c0\u03af\u03c4\u03b9\u00bb: \u03a4\u03b6\u03b1\u03b6 \u03c4\u03b1\u03be\u03af\u03b4\u03b9\u03b1 \u03b1\u03c0\u03cc \u03c4\u03bf \u039a\u03ad\u03bd\u03c4\u03c1\u03bf \u03a0\u03bf\u03bb\u03b9\u03c4\u03b9\u03c3\u03bc\u03bf\u03cd ..."
    }

【问题讨论】:

  • 它看起来像 unicode。你只需要打印出来。
  • 字符串"\u0398\u03b5\u03b1\u03c4\u03c1\u03b9\u03ba\u03cc"字面意思与字符串"Θεατρικό" 相同。你究竟是如何得到字符串的,你是如何查看它的?你想要的结果到底是什么?
  • 嘿@quamrana,我知道。但我需要将它保存在文件中,而不仅仅是打印它。再次查看帖子,我刚刚编辑了它。
  • 如果您使用json.dumps()result(我假设它是listdict)转换为json 字符串,那么无论它在哪里都无法读取当它包含 unicode 字符串时。
  • 嘿,@quamrana! 2话要说。首先,它是 dict 是的,但我可以使用 str() 将其转换为 str 。其次,我不敢相信没有办法保存在非编码的 txt 文件中。当我尝试打印字符串时,我可以阅读所有内容。这意味着python在显示之前会转换文本,我只需要在将字符串保存到txt文件时进行相同的转换。

标签: python text


【解决方案1】:

默认情况下,json 模块会转义所有非 ascii 字符。使用 ensure_ascii=False 保持所有 unicode 字符不转义:

>>> print(json.dumps("""{"date": "Πριν από 3 ώρες"}"""))
"{\"date\": \"\u03a0\u03c1\u03b9\u03bd \u03b1\u03c0\u03cc 3 \u03ce\u03c1\u03b5\u03c2\"}"
>>> print(json.dumps("""{"date": "Πριν από 3 ώρες"}""", ensure_ascii=False))
"{\"date\": \"Πριν από 3 ώρες\"}"

转储数据时只需传递参数:

with open("result.txt", "a+", encoding="utf-8-sig") as f:
    json.dump(result, f, indent=2, sort_keys=True)

请注意,就 JSON 标准而言,带和不带非 ascii 转义的 JSON 是等效的。

【讨论】:

猜你喜欢
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2017-05-29
相关资源
最近更新 更多