JSON解析器Python脚本问题

2024-04-25 07:05:49 发布

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

我是CS的一年级学生,尝试调试一个简单的Python脚本。在

该脚本试图解析JSON文件的目录,也就是AWS存储桶。但是,我不知道这些错误是从哪里来的:

import json
import os
from pprint import pprint

jsonDirectory = "/path/to/dir/"
targetRegion = "-insert-region-here"

print("Searching for records with AWS Region: " + targetRegion)
print("")

for filename in os.listdir(jsonDirectory):
print("Reading: " + filename)
data = json.dumps(open(jsonDirectory + filename))

for i in range(len(data["Records"])):
    if data["Records"][i]["awsRegion"] == targetRegion:
        print("---------------------------")
        print("Record #" + str(i))
        print("Username: " + data["Records"][i]["userIdentity"]    ["userName"])
        print("Event name: " + data["Records"][i]["eventName"])
        print("Event time: " + data["Records"][i]["eventTime"])
        print("---------------------------")

print("")

print("Completed reading files.")

错误:

Traceback (most recent call last): File "/path/to/file.py", line 13, in data = json.dumps(open(jsonDirectory + filename)) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/init.py", line 231, in dumps return _default_encoder.encode(obj) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default o.class.name) TypeError: Object of type 'TextIOWrapper' is not JSON serializable


Tags: inpyjsondataliblinelibraryframework
1条回答
网友
1楼 · 发布于 2024-04-25 07:05:49

让我假设您不在西欧或美国,并且默认编码不是UTF-8或者通常非常兼容的东西,比如iso-8859-1。从上面的评论来看

data = json.load(open(jsonDirectory + filename))

如果你把这句话分开:

^{pr2}$

您将看到错误发生在fdata = f.read()。建议是:

^{3}$

如果您不确定,请尝试强制open忽略/绕过错误。来自Python文档:https://docs.python.org/3/library/functions.html#open

errors is an optional string that specifies how encoding and decoding errors are to be handled—this cannot be used in binary mode. A variety of standard error handlers are available (listed under Error Handlers), though any error handling name that has been registered with codecs.register_error() is also valid. The standard names include:

  • 'strict' to raise a ValueError exception if there is an encoding error. The default value of None has the same effect.
  • 'ignore' ignores errors. Note that ignoring encoding errors can lead to data loss.
  • 'replace' causes a replacement marker (such as '?') to be inserted where there is malformed data.
  • 'surrogateescape' will represent any incorrect bytes as code points in the Unicode Private Use Area ranging from U+DC80 to U+DCFF. These private code points will then be turned back into the same bytes when the surrogateescape error handler is used when writing data. This is useful for processing files in an unknown encoding.
  • 'xmlcharrefreplace' is only supported when writing to a file. Characters not supported by the encoding are replaced with the appropriate XML character reference &#nnn;.
  • 'backslashreplace' replaces malformed data by Python’s backslashed escape sequences.
  • 'namereplace' (also only supported when writing) replaces unsupported characters with \N{...} escape sequences.

ignore开始,如下所示:

f = open(jsonDirectory + filename, errors='ignore')
fdata = f.read()
data = json.loads(fdata)

并检查输出是否满足您的要求或哪里出了问题。在

相关问题 更多 >