【问题标题】:Don't understand this TypeError while attempting to parse JSON file尝试解析 JSON 文件时不理解此 TypeError
【发布时间】:2019-03-05 20:20:11
【问题描述】:

我在尝试读取和解析 json 文件时收到错误 TypeError: expected str, bytes or os.PathLike object, not NoneType。我在网上检查了这个错误,并在stackoverflow上偶然发现了这个链接-

TypeError: expected str, bytes or os.PathLike object, not _io.BufferedReader

但上面看起来像同样的问题,但解决方案不适用于我的代码。

代码如下:

# function to parse the json file and extract useful information
def json_parser(file):
    print(file)
    with open(file, 'r') as fp:
        json_decode = json.loads(fp.read())
        pprint(json_decode)
        return json_decode

# script to read through the directories and parse json files

rootDir = 'projects/dataset'
for dirName, subdirList, fileList in os.walk(rootDir):
    print("Found directory: %s" % dirName)
    for fname in fileList:
        print('\t%s' % fname)
        fname1 = print("'" + fname + "'")
        # calling the json parser function defined above
        result = json_parser(fname1)
        print(result)

追溯:

Error: File "test_json_parsing.py", line 15, in json_parser
    with open(file, 'r') as fp:
TypeError: expected str, bytes or os.PathLike object, not NoneType

【问题讨论】:

  • @py_noob 你能告诉我们你是怎么打电话给json_parser()的吗?
  • fname1 = os.path.join(dirName, fname)

标签: python typeerror


【解决方案1】:

fname1 = print("'" + fname + "'")None 分配给 fname1 而不是打印的字符串。因此,如错误所示,fname1 属于NoneType,无法打开。

编辑感谢 @Jared Smith 为我解决这个问题

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多