以utf-8形式進行保存和讀取;
import json
import io
dicts = {"name": "lucy", "sex": "boy"}
json_dicts = json.dumps(dicts, indent=4)
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))
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)