Python读写CSV与JSON文件方法

1. CSV文件读写

使用内置的csv模块

python 复制代码
import csv

# 写入CSV文件
def write_csv_file():
    # 准备数据
    data = [
        ['姓名', '年龄', '城市'],
        ['张三', 25, '北京'],
        ['李四', 30, '上海'],
        ['王五', 28, '深圳']
    ]
    
    # 写入CSV文件
    with open('data.csv', 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerows(data)
    print("CSV文件写入完成")

# 读取CSV文件
def read_csv_file():
    print("读取CSV文件内容:")
    with open('data.csv', 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        for row in reader:
            print(row)

# 使用字典方式读写(更常用)
def write_csv_dict():
    data = [
        {'name': '张三', 'age': 25, 'city': '北京'},
        {'name': '李四', 'age': 30, 'city': '上海'},
        {'name': '王五', 'age': 28, 'city': '深圳'}
    ]
    
    with open('data_dict.csv', 'w', newline='', encoding='utf-8') as f:
        # 表头就是字典的键
        fieldnames = ['name', 'age', 'city']
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()  # 写入表头
        writer.writerows(data)
    print("字典方式CSV写入完成")

def read_csv_dict():
    print("字典方式读取CSV:")
    with open('data_dict.csv', 'r', encoding='utf-8') as f:
        reader = csv.DictReader(f)
        for row in reader:
            print(f"姓名:{row['name']}, 年龄:{row['age']}, 城市:{row['city']}")

# 执行示例
write_csv_file()
read_csv_file()
write_csv_dict()
read_csv_dict()

使用pandas库(更强大)

python 复制代码
import pandas as pd

def pandas_csv_demo():
    # 创建DataFrame
    data = {
        '姓名': ['张三', '李四', '王五'],
        '年龄': [25, 30, 28],
        '城市': ['北京', '上海', '深圳']
    }
    df = pd.DataFrame(data)
    
    # 写入CSV
    df.to_csv('pandas_data.csv', index=False, encoding='utf-8')
    print("pandas CSV写入完成")
    
    # 读取CSV
    df_read = pd.read_csv('pandas_data.csv')
    print("\n读取的数据:")
    print(df_read)
    
    # 查看前几行
    print("\n前2行数据:")
    print(df_read.head(2))
    
    # 数据处理示例
    print("\n年龄大于26的数据:")
    filtered = df_read[df_read['年龄'] > 26]
    print(filtered)

pandas_csv_demo()

2. JSON文件读写

使用内置的json模块

python 复制代码
import json

def write_json_file():
    # Python字典/列表数据
    data = {
        "employees": [
            {
                "name": "张三",
                "age": 25,
                "department": "技术部",
                "skills": ["Python", "Java", "数据库"]
            },
            {
                "name": "李四",
                "age": 30,
                "department": "市场部",
                "skills": ["市场营销", "数据分析"]
            }
        ],
        "company": {
            "name": "示例公司",
            "location": "北京",
            "established": 2010
        }
    }
    
    # 写入JSON文件
    with open('data.json', 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=2)  # indent美化格式
    print("JSON文件写入完成")

def read_json_file():
    # 读取JSON文件
    with open('data.json', 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    print("JSON文件内容:")
    print(f"公司名称:{data['company']['name']}")
    print(f"员工数量:{len(data['employees'])}")
    
    print("\n员工列表:")
    for employee in data['employees']:
        print(f"姓名:{employee['name']}, 部门:{employee['department']}")

# JSON字符串与Python对象转换
def json_string_demo():
    # Python对象转JSON字符串
    data = {
        'name': '王五',
        'age': 28,
        'married': False,
        'hobbies': ['读书', '游泳']
    }
    
    # 转换为JSON字符串
    json_str = json.dumps(data, ensure_ascii=False, indent=2)
    print("JSON字符串:")
    print(json_str)
    
    # JSON字符串转Python对象
    python_obj = json.loads(json_str)
    print("\n转换回Python对象:")
    print(f"姓名:{python_obj['name']}")
    print(f"爱好:{', '.join(python_obj['hobbies'])}")

# 执行示例
write_json_file()
read_json_file()
json_string_demo()

使用pandas处理JSON

python 复制代码
import pandas as pd

def pandas_json_demo():
    # 读取JSON文件
    df = pd.read_json('data.json')
    print("pandas读取JSON:")
    print(df)
    
    # 或者读取特定路径
    df_employees = pd.json_normalize(
        json.load(open('data.json', encoding='utf-8'))['employees'],
        'skills',  # 展开skills数组
        ['name', 'age', 'department']  # 保留其他字段
    )
    print("\n展开后的数据:")
    print(df_employees)
    
    # DataFrame转JSON
    df_simple = pd.DataFrame({
        'name': ['张三', '李四'],
        'score': [85, 92]
    })
    
    # 转换为JSON字符串
    json_str = df_simple.to_json(orient='records', force_ascii=False, indent=2)
    print("\nDataFrame转JSON:")
    print(json_str)

pandas_json_demo()

3. 综合示例:CSV和JSON互相转换

python 复制代码
import csv
import json

def csv_to_json():
    """将CSV文件转换为JSON格式"""
    csv_file = 'data_dict.csv'
    json_file = 'converted.json'
    
    data = []
    with open(csv_file, 'r', encoding='utf-8') as f:
        reader = csv.DictReader(f)
        for row in reader:
            # 转换年龄为整数
            row['age'] = int(row['age'])
            data.append(row)
    
    with open(json_file, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
    
    print(f"已将 {csv_file} 转换为 {json_file}")

def json_to_csv():
    """将JSON文件转换为CSV格式"""
    json_file = 'data.json'
    csv_file = 'converted.csv'
    
    with open(json_file, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # 提取员工数据
    employees = data['employees']
    
    with open(csv_file, 'w', newline='', encoding='utf-8') as f:
        if employees:
            fieldnames = employees[0].keys()
            writer = csv.DictWriter(f, fieldnames=fieldnames)
            writer.writeheader()
            writer.writerows(employees)
    
    print(f"已将 {json_file} 中的员工数据转换为 {csv_file}")

# 执行转换
csv_to_json()
json_to_csv()

4. 处理复杂JSON结构

python 复制代码
import json

def handle_complex_json():
    """处理复杂的JSON结构"""
    
    # 嵌套的JSON数据
    complex_data = {
        "project": "数据分析平台",
        "version": "1.0",
        "config": {
            "database": {
                "host": "localhost",
                "port": 5432,
                "tables": ["users", "products", "orders"]
            },
            "api_settings": {
                "rate_limit": 100,
                "timeout": 30
            }
        },
        "users": [
            {"id": 1, "role": "admin", "permissions": ["read", "write", "delete"]},
            {"id": 2, "role": "user", "permissions": ["read"]}
        ]
    }
    
    # 写入文件
    with open('config.json', 'w', encoding='utf-8') as f:
        json.dump(complex_data, f, ensure_ascii=False, indent=2)
    
    # 读取并修改
    with open('config.json', 'r', encoding='utf-8') as f:
        config = json.load(f)
    
    # 修改配置
    config['config']['database']['port'] = 5433
    config['users'].append({"id": 3, "role": "editor", "permissions": ["read", "write"]})
    
    # 保存修改
    with open('config_modified.json', 'w', encoding='utf-8') as f:
        json.dump(config, f, ensure_ascii=False, indent=2)
    
    print("复杂JSON处理完成")

handle_complex_json()

主要方法总结

操作 CSV JSON
读取 csv.reader() / csv.DictReader() json.load()
写入 csv.writer() / csv.DictWriter() json.dump()
字符串转换 - json.loads() / json.dumps()
pandas读取 pd.read_csv() pd.read_json()
pandas写入 df.to_csv() df.to_json()

最佳实践建议

  1. 编码问题 :始终指定编码(建议使用utf-8
  2. CSV换行 :写入时使用newline=''避免空行
  3. JSON格式化 :使用indent参数使JSON更易读
  4. 中文处理 :JSON使用ensure_ascii=False
  5. 异常处理:添加try-except处理文件不存在等情况
  6. 大文件处理:使用逐行读取或pandas的chunksize参数
相关推荐
ranchor6662 小时前
df赋值和.copy的区别(SettingWithCopyWarning)
大数据·人工智能·python
2401_841495642 小时前
【LeetCode刷题】合并区间
数据结构·python·算法·leetcode·合并·遍历·排序
Q_Q5110082852 小时前
python+springboot+django/flask基于数据挖掘的高考志愿推荐系统
spring boot·python·django·flask·node.js·php
weixin_307779132 小时前
Jenkins jQuery3 API 插件详解:赋能插件前端开发的利器
运维·开发语言·前端·jenkins·jquery
爱笑的眼睛112 小时前
神经网络的骨架:深入解析前向传播的数学本质与工程实现
java·人工智能·python·ai
木头左2 小时前
层次化注意力分配策略在量化交易中的实现与应用
python
Q_Q5110082852 小时前
python+springboot+django/flask基于用户评论主题挖掘的旅游景点推荐系统
spring boot·python·django·flask·node.js
世转神风-2 小时前
QEventLoop-qt阻塞异步操作
开发语言·qt
Hard but lovely2 小时前
C++ 11--》初始化
开发语言·c++