python保存为json格式后打开,部分json格式提升编码错误
从b站爬取了相关视频文件得详细信息,另存为后用pandas打开比如附件中的动画-综合-1312页.json,会报如下错误,但是动画-综合-1313页.json又能正常保存。Traceback (most recent call last):
File "D:xxxx.py", line 58, in <module>
数据.to_csv('数据信息调2223.csv', index=False, header=0, mode='a', encoding='ANSI')
File "C:\Python38\lib\site-packages\pandas\core\generic.py", line 3466, in to_csv
return DataFrameRenderer(formatter).to_csv(
File "C:\Python38\lib\site-packages\pandas\io\formats\format.py", line 1105, in to_csv
csv_formatter.save()
File "C:\Python38\lib\site-packages\pandas\io\formats\csvs.py", line 257, in save
self._save()
File "C:\Python38\lib\site-packages\pandas\io\formats\csvs.py", line 262, in _save
self._save_body()
File "C:\Python38\lib\site-packages\pandas\io\formats\csvs.py", line 300, in _save_body
self._save_chunk(start_i, end_i)
File "C:\Python38\lib\site-packages\pandas\io\formats\csvs.py", line 311, in _save_chunk
libwriters.write_csv_rows(
File "pandas\_libs\writers.pyx", line 72, in pandas._libs.writers.write_csv_rows
File "C:\Python38\lib\encodings\mbcs.py", line 25, in encode
return mbcs_encode(input, self.errors)
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character
但是部分json格式又没有问题,系统也重装了问题依旧。
你的json是utf-8编码的,用utf-8。
import json
with open('test.json','r', encoding='utf-8') as f:
data = json.loads(f.read())
print(data)
print(data["name"]) suchocolate 发表于 2021-8-19 11:20
你的json是utf-8编码的,用utf-8。
兄弟主要用pandas另存为excel会报错,pandas读取都没有问题,问题在另存为 # -*- coding: utf-8 -*-
import json
import pandas as pd
from codecs import mbcs_encode
def encode(self, input, final=False):
return mbcs_encode(input, self.errors)
with open('动画-特摄-1页.json', 'r',encoding='utf-8') as f:
data=json.loads(f.read())
df=pd.DataFrame(data["result"])
#df.to_csv('aaaaa.csv', encoding='ANSI')
兄弟这是原代码
页:
[1]