在進行大量資料讀寫時,一般json不能讀寫特別大的資料,此時需要按行來進行讀寫:
# 按行寫檔案
import json
data = [
,]for item in data:
with open('data.json', 'a+', encoding='utf-8') as f:
line = json.dumps(item, ensure_ascii=false)
f.write(line+'\n')
# 按行讀取檔案
import json
data =
with open('data.json', 'r', encoding="utf-8") as f:
# 讀取所有行 每行會是乙個字串
for j in f.readlines():
print(j)
print(type(j))
# 將josn字串轉化為dict字典
j = json.loads(j)
print(type(j))
print(data)
[, ]
python 檔案按行讀寫
1 w 寫模式,它是不能讀的,如果用w模式開啟乙個已經存在的檔案,會清空以前的檔案內容,重新寫 w 是讀寫內容,只要沾上w,肯定會清空原來的檔案 2 r 讀模式,只能讀,不能寫,而且檔案必須存在 r 是讀寫模式,只要沾上r,檔案必須存在 3 a 追加模式,也能寫,在檔案的末尾新增內容 4 rb wb...
Csdiofile 讀 按行讀寫檔案
endif cstdiofile file file.open t test.txt cfile modecreate cfile modenotruncate cfile modereadwrite if 0 檔案開啟模式可組合使用,用 隔開,常用的有以下幾種 cfile modecreate 以...
json讀寫檔案
jsonc 寫配置檔案比較簡單,並且解析配置檔案也比較省事。寫配置檔案 cpp view plain copy include include include include include inc json.h define config file config.json typedef stru...