數據寫出到json

字典類型數據寫出到json

import json
def write_to_file(content, filename):
    with open(filename, 'a', encoding='utf-8') as f:
        f.write(json.dumps(content, ensure_ascii=False) + '\n')

DataFrame類型數據寫出到json

import json
def df2json(df,filepath):
    with open(filepath,'a+',encoding='utf-8') as f:
        for i in df.columns:
            df[i] = df[i].astype('str')
        contents = df.to_dict('records')
        for index,content in enumerate(contents):
            try:
                f.write(json.dumps(content,ensure_ascii=False) + '\n')
            except:
                print(F'{index+1},{content} 無法寫出')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章