处理文本文档清洗数据并写入csv

处理文本文档清洗数据并写入csv

# coding=utf-8
import json
import re
import csv
import pandas as pd

with open('日数据2021-2022.txt', 'r', encoding='utf-8') as f:
    data = f.read()
    # 正则匹配
    data1 = re.findall(r'{".*?null}', data)
    for data in data1:
        rep = data.replace("null", "-99").replace(' ', "").replace('{"msg":"success","code":0,"data":{"top":[', "")
        rep1 = eval(rep)
        file = open('456.csv', 'w', encoding='utf-8', newline='')
        csv_writer = csv.DictWriter(file, fieldnames=list(rep1.keys()))
        csv_writer.writeheader()
        for i in rep1:  # 将字典逐行写入csv
            dic1 = {key: rep1[key][i] for key in rep1.keys()}
            csv_writer.writerow(dic1)
        file.close()

你可能感兴趣的:(python笔记,python,pandas,数据分析)