类型错误:不支持解码str(Python 3.4)

2024-07-07 06:52:19 发布

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

我在尝试将bytes对象转换为字符串时收到一个奇怪的错误:

    Traceback (most recent call last):
  File "/home/Scraper_v1.0.py", line 149, in <module>
    rawjsonstr = str(rawjsonstr, 'utf-16')
TypeError: decoding str is not supported

我的代码如下:

# preparing string
            try:
                rawjsonstr = "".join(rawjson[3]).encode('utf-16')  # used to select the correct text/javascript node
            except IndexError:
                errorcount += 1
                with open(filepath, "at") as f:
                    write = csv.writer(f)
                    write.writerow(["Error: no valid JSON found",
                                    statenum,
                                    statesubnum,
                                    suburbnum,
                                    listingnum,
                                    listingsurlstr])
            pass

            rmvlist = ["var digitalData = ",
                        "var titanEnabled = true;",
                        "titan = titan || {};",
                        "// enable the command queue to allow spa execution in the correct order",
                        "titan.cmd = titan.cmd || [];"]

            rawjsonstr = str(rawjsonstr, 'utf-16')

编辑

在不编码/解码的情况下重新运行上述代码。现在在同一个循环上接收以下错误。

Traceback (most recent call last):
  File "/home/isaac/PycharmProjects/ResidentialData/AllHomesScraper_v1.0.py", line 162, in <module>
    jsondata = json.loads(rawjsonstr)
  File "/usr/lib64/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python3.4/json/decoder.py", line 359, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 1 column 1033 (char 1032)

我将重新运行它并捕获导致此失败的字符串(看起来它只是格式不正确的JSON数据)。

编辑:下面提供的解决方案。


Tags: theinpyjsonusrlineutffile
1条回答
网友
1楼 · 发布于 2024-07-07 06:52:19

该错误是由于程序试图分析无效的JSON字符串而导致的:JSON formatter

解决方案:捕获错误,并在发生错误时在字符串末尾附加一个{'。

相关问题 更多 >