CVS文件转Json格式

需求

读取CSV文件,将数据组合成新的JSON格式输出打印

如果出现不足5列的,空的既跳过

测试用例

bash 复制代码
import csv

def process_csv_file(filename):
    """
    处理CSV文件,获取C列(domain)和E列(valid)的值
    注意:CSV列索引从0开始,C列是第3列(索引2),E列是第5列(索引4)
    """
    results = []
    
    with open(filename, 'r', encoding='utf-8') as file:
        # 创建CSV阅读器
        csv_reader = csv.reader(file)
        
        # 跳过表头(如果有)
        headers = next(csv_reader, None)
        print(f"CSV表头: {headers}")
        
        # 处理每一行
        for row_num, row in enumerate(csv_reader, start=2):  # 行号从2开始(跳过表头)
            if len(row) >= 5:  # 确保行至少有5列
                domain = row[2]  # C列,索引2
                valid = row[4]   # E列,索引4
                
                results.append({
                    'row': row_num,
                    'domain': domain,
                    'valid': valid
                })
            else:
                print(f"警告: 第{row_num}行列数不足,跳过")
    
    return results


if __name__ == '__main__':
    # 使用示例
    results = process_csv_file('risk_mail_domain_verify_record_202602041439.csv')
    for result in results:
        print(f"行 {result['row']}: domain={result['domain']}, valid={result['valid']}")
相关推荐
学术阿凡提5 小时前
Spring Boot 集成 Fastjson2 完整教程:从入门到避坑
spring boot·安全·json
LIUAWEIO5 小时前
鸽鸽工具网:免费在线工具大全,打开网页即用
人工智能·安全·ai·json
半天法师10 小时前
Bug 记录:UE 结构体转 JSON 时 Key 字段大小写异常 (Editor 与打包后表现不一致)
ai·ue5·json·bug
鸽芷咕11 小时前
KingbaseES数据类型完全指南:从基础CHAR到JSON/XML/几何类型
xml·oracle·json
归途醉染1 天前
Swifter.Json
c#·json·swifter.json
网络点点滴1 天前
NPM 和 package.json 文件简介
前端·npm·json
夜瞬1 天前
HTTP基础教程:请求方法、状态码、JSON、鉴权、超时、重试与流式返回
网络协议·http·json
wtsolutions2 天前
JSON-to-Excel 本地化应用发布:安全离线转换,数据零泄露
安全·json·excel
剑神一笑3 天前
从 JSON.parse 到树形视图:实现一个在线 JSON 格式化工具
前端·javascript·json
烤麻辣烫3 天前
json与fastjson
前端·javascript·学习·json