跳转至

读写各种格式

程序运行的过程中,所有的变量都是在内存中临时存储,把内存中的变量变为可存储或者可传输的,然后才能存储到磁盘或者数据交换,这个过程称之为序列化,反之,称为反序列化

  • dump 存/写 序列化
  • load 取/读 反序列化

Pickle格式

Python特有,不通用且不可阅读

import pickle

# 从内存中取出
pickle.loads(y)


# 从文件中取出,必须是“rb”模式
with open("test.pickle", "rb") as f:
    pickle.load(f)
import pickle

# 存到内存
y = pickle.dumps(x)


# 存储到文件,必须是“wb”模式
with open("test.pickle", "wb") as f:
    pickle.dump(x, f)

读写Json

import json

# 从内存中读取
str = "{'a':1, 'b':2}"
"""
需要注意的是,这里字符串类型的dict,key如果为单引号,loads时会报错,需要转换兼容一下
方式1:new_str = str.replace('\'', '\"')
方式2:new_str = json.dumps(ast.literal_eval(str))  # 需要 import ast
"""
json.loads(new_str)


# 从文件中读取
with open(file_path, "r", encoding='UTF-8') as f:
    result = json.load(f)
import json

# 存到内存
cache = json.dumps(x)


# 序列化并存储到json文件
with open(file_path, "w", encoding='UTF-8') as f:
    json.dump(x, f, indent=4, ensure_ascii=False, sort_keys=True, separators=(',', ':'))
    """
    :indent  设置缩进显示成几个空格
    :ensure_ascii  是否显示ascii码,默认为true,如果要显示中文需要设置为false
    :sort_keys  是否按照字母排序
    :separators  设置分隔符
    """

读写YAML

pip install pyyaml

import yaml

def read_from_yaml(file_path):
    with open(file_path, "r", encoding="utf-8") as f:
        return yaml.safe_load(f.read())
import yaml

def write_to_yaml(content, file_path):
    with open(file_path, "w", encoding="utf-8") as f:
        # allow_unicode=True 保证中文写入正常显示
        yaml.dump(content, f, default_flow_style=False, encoding='utf-8', allow_unicode=True)
  • 修改
data = read_from_yaml(file_path)
data["key"] = value
write_to_yaml(data, file_path)

读写TOML

pip install toml

import toml

# 读
with open(file_path, "r", encoding='UTF-8') as f:
    content = toml.load(file_path)

# 写
with open(file_path, "w", encoding='UTF-8') as f:
    toml.dump(content, f)

读写CSV

import csv

# 普通方式
with open("test.csv", "r") as csv_file:
    csv_reader = csv.reader(csv_file)
    headers = next(csv_reader)  # 使用next()函数将迭代器先迭代一次,跳过表头
    for row in csv_reader:
        print(row[0], row[1])


# 字典方式(推荐)
with open("test.csv", "r") as csv_file:
    csv_reader = csv.DictReader(csv_file)
    # print(csv_reader.fieldnames)  # 输出表头:['a', 'b']
    result = {}
    for item in csv_reader:
        result[item["a"]] = item["b"]
    print(result)  # {'a1': 'b1', 'a2': 'b2'}
import csv

# 普通方式
# 如果要追加写入,则以“a”模式打开
# 不加newline每条数据中间会出现空行
with open("test.csv", "w", newline='') as csv_file:
    csv_writer = csv.writer(csv_file)
    # 定义表头
    fieldnames = ["a","b"]
    # 写入表头
    csv_writer.writerow(fieldnames)
    # 写入数据
    csv_writer.writerow(['a1', 'b2'])

# 字典方式
with open("test.csv", "w", newline='') as csv_file:
    # 定义表头
    fieldnames = ["a","b"]
    # delimiter参数可指定分隔符,默认为逗号
    csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    # 写入表头
    csv_writer.writeheader()
    # 写入数据
    csv_writer.writerow({"a":a1, "b":b1})

读写ini

Windows不要使用中文注释

import configparser

# 创建管理对象
conf = configparser.ConfigParser()
# 读ini文件
conf.read(conf_path, encoding="utf-8")
# 获取所有的section, 返回list
sections = conf.sections()
# 获取某个section下所有(key:value),每一对用一个元组表示,返回一个元组list
items = conf.items('edit_page')
menu = conf.get('edit_page','menu')

print(sections)
print(items)
print(items[0])
print(items[0][1])
print(menu)

最后更新: 2022-03-16