数据处理 `.txt`, `.json`, `.csv`, `.excel`, `.pkl` 相互转化

数据处理

这里主要整理的是.txt, .json, .csv, .excel, .pkl 文件之间的数据存储和转化

1. txt部分

import json


def save_json_list_txt(json_data, target_path):
    """ 保存json列表到txt文件中
    """
    with open(target_path, "w", encoding="utf-8") as file:
        for item in json_data:
            item = str(item).replace("\n", "\\n")
            file.write(item)
            # file.write(str(item))
            file.write("\n")


def load_json_list_txt(source_path):
    """ 从txt文件中加载json列表
    """
    json_list = []
    try:
        with open(source_path, "r", encoding="utf-8") as file:
            for line in file:
                try:
                    json_data = eval(line.strip())
                    json_list.append(json_data)
                except json.JSONDecodeError:
                    print(f"无法解析Json数据: {line.strip()}")
            return json_list
    except FileNotFoundError:
        print(f"文件 '{source_path} 未找到'")


def save_str_list_txt(str_list, target_path):
    """ 保存str列表到txt文件中
    """
    with open(target_path, "w", encoding="utf-8") as file:
        for item in str_list:
            item = item.replace("\n", "\\n")
            file.write(item)
            file.write("\n")


def load_str_list_txt(file_path):
    """ 从txt文件中加载str列表
    """
    txt_list = []
    try:
        with open(file_path, "r", encoding="utf-8") as file:
            for line in file.readlines():
                txt_list.append(line.strip())
            return txt_list
    except FileNotFoundError:
        print(f"文件 ‘{file_path} 未找到’")




if __name__ == '__main__':

    human_list = [
        {
            "name": "jordan\n",
            "age": 30,
            "city": "南昌",
            "list": [3, 6, 0]
        },
        {
            "name": "John",
            "age": 30,
            "city": "广州",
            "list": [3, 6, 1]
        },
        {
            "name": "JiWang",
            "age": 30,
            "city": "深圳",
            "list": [2, 3, 1]
        },
    ]
    day_list = ["星期一\n\n", "星期二", "星期三", "星期四", "星期⑤", "星期六", "星期天"]

    save_json_list_txt(human_list, "human_list.txt")
    new_human_list = load_json_list_txt("human_list.txt")
    print(new_human_list)
    # print(type(new_human_list))
    # print(type(new_human_list[0]))
    save_str_list_txt(day_list, "day.txt")
    new_day_list = load_str_list_txt("day.txt")
    print(new_day_list)

2. json格式部分

import json


def save_json(json_list, target_path):

    with open(target_path, "w", encoding="utf-8") as json_file:
        json.dump(json_list,  json_file, indent=4)
    print(f"Json列表已经保存到 {target_path} 文件中。")

def load_json(source_file):
    json_list = []
    with open(source_file, "r", encoding="utf-8") as json_file:
        try:
            json_list = json.load(json_file)
        except json.JSONDecodeError as e:
            print(f"JSON解析错误: {e}")
        return json_list


def save_json_line(json_list, target_path):
    with open(target_path, 'w', encoding="utf-8") as json_file:
        for item in json_list:
            json.dump(item, json_file)
            json_file.write("\n")
    print(f"Json列表已经保存到 {target_path} 文件中。 每一行为一个json对象")


def load_json_line(source_file):
    json_list = []
    with open(source_file, "r", encoding="utf-8") as json_file:
        for line in json_file:
            try:
                json_data = json.loads(line.strip())
                json_list.append(json_data)
            except json.JSONDecodeError as e:
                print(f"JSON解析错误: {e}")
        return json_list


if __name__ == '__main__':

    human_list = [
        {
            "name": "jordan",
            "age": 30,
            "city": "南昌",
            "list": [3, 6, 0]
        },
        {
            "name": "John",
            "age": 30,
            "city": "广州",
            "list": [3, 6, 1]
        },
        {
            "name": "JiWang",
            "age": 30,
            "city": "深圳",
            "list": [2, 3, 1]
        },
    ]
    human = {
            "name": "jordan",
            "age": 30,
            "city": "南昌",
            "list": [3, 6, 0]
        }

    json_path = "json/human_line_list.json"
    save_json_line(human_list, json_path)
    new_human_list = load_json_line(json_path)
    print(new_human_list)
    # save_json(human_list, "json/human_list.json")
    # human_json = load_json("json/human_list.json")
    # print(human_json)

    save_json(human, "json/human.json")
    human_json = load_json("json/human.json")
    print(human_json)
    # new_json_list = load_json_list(json_path)
    # print(new_json_list)
    # # print(type(new_human_list))
    # # print(type(new_human_list[0]))
    # save_str_list_txt(day_list, "day.txt")
    # new_day_list = load_str_list_txt("day.txt")
    # print(new_day_list)






你可能感兴趣的:(数据处理/分析,json,excel,windows)