尝试将字节对象转换为字符串时,我收到一个奇怪的错误:
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数据)。
编辑:下面提供了解决方案。