以utf-8形式进行保存和读取;
import json
import io
dicts = {"name": "lucy", "sex": "boy"}
json_dicts = json.dumps(dicts, indent=4,ensure_ascii=False)
print(json_dicts)
testjsonoutputpath="./data/testjsonoutput.json"
with io.open(testjsonoutputpath,"w",encoding='utf-8') as f:
f.write(json.dumps(dicts,indent=4).decode('utf-8'))
#loads:
with io.open(testjsonoutputpath,"r",encoding='utf-8') as f:
prelist=json.loads(f.read().encode("utf-8"))
#or
#prelist=json.load(f)
print(prelist)
#最简单的json写入操作
with open("./waittochange/changedpre/"+path[15:], "w") as f:
f.write(json.dumps(allframe, indent=4,ensure_ascii=False))
with open('frameres.json', "r") as f:
prelist = json.loads(f.read())
json中有中文导致生成的json文件乱码?:
with io.open(testjsonoutputpath, "w",encoding='utf8') as f:
f.write(json.dumps(reslist, indent=4,ensure_ascii=False))
pickle操作:
import pickle
data1 = {'a': [1, 2.0, 3, 4+6j],
'b': 3.4,
'c': {1:"sfsf"}}
output = open('data.pkl', 'wb')
# Pickle dictionary using protocol 0.
pickle.dump(data1, output)
output.close()
import pprint, pickle
pkl_file = open('data.pkl', 'rb')
data1 = pickle.load(pkl_file)
pprint.pprint(data1)
pkl_file.close()
python2下生成的pickle在python3中无法打开?:
在python3中
fr = open(path, 'rb')
mytree = pickle.load(fr,encoding='latin1')
return mytree
csv操作:
使用pandas
参考:
https://blog.csdn.net/qq_16949707/article/details/76099310
https://blog.csdn.net/whut_ldz/article/details/78921947
csv筛选
https://www.jianshu.com/p/61add27e62dd
检查是否没有符合的条件:
thisframedata=thispickledata[psvdata['frame_id']==55]
print(thisframedata.head().empty)
csv筛选字符串类型
#筛选出该列含有findname字符串的
finddata=psvdata[psvdata['pickle_file'].str.contains(findname)]
写入csv中文乱码
#coding=utf8
import codecs
out = open(folderpath+csvname, 'a', newline='',encoding='utf-8-sig')
title = ['教室', '日期', '小节节次', '选课人数', '课程名称', '教师姓名', '课内时刻', '人工识别出勤人数', '系统计算出勤人数',
'系统计算出勤率', '人工识别核心区域人数', '系统计算核心区人数', '系统计算核心区上座率', '人工识别核心区抬头率',
'系统计算核心区域抬头率', '课堂抬头率']
csv_write.writerow(title)