python操作.csv文件

这篇博客介绍了如何使用Python处理CSV文件,特别是针对具有相同手机号的数据行,当发现任意一行发送成功后,将所有相同手机号的行状态都更新为发送成功。详细步骤和代码示例提供。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考链接:

https://blog.youkuaiyun.com/weixin_36279318/article/details/79078255

需求:将a.csv中,相同手机号,只要有一个发送成功,则其余改为发送成功

代码:

# -*- coding: utf-8 -*-
# @Time    : 2019/2/19 16:32
# @Author  : guigle
# @File    : excel_check.py
# @Software: PyCharm
import os
from collections import defaultdict
import csv

# 遍历软件运行的文件夹
path_dir = os.getcwd()
for root, dirs, files in os.walk(path_dir):
    for file in files:
        if file.endswith('.csv'):
            f = open(file, encoding='gbk')
            reader1 = csv.reader(f)
            # 电话列
            tel_col = [column[2] for column in reader1]
            tel_li = tel_col[1:]
            # print(tel_li)
            f.close()

            # 错误描述列
            f = open(file, encoding='gbk')
            reader2 = csv.reader(f)
            result_col = [column[6] for column in reader2]
            result_li = result_col[1:]
            # print(result_li)
            f.close()

            # 拿到电话号码,及其对应的索引列表
            tel = defaultdict(list)
            tel_dic = {}
            for k, va in [(v, i) for i, v in enumerate(tel_li)]:
                tel[k].append(va)
                tel_dic = dict(tel)
                # print(tel_dic)

            # res = defaultdict(list)
            # res_dic = {}
            # for k, va in [(v, i) for i, v in enumerate(result_li)]:
            #     res[k].append(va)
            #     res_dic = dict(res)
            # print(dict(d))

            for v in tel_dic.values():
                print(v)
                # 索引列表,每个索引列表v对应一个空列表
                index_li = []
                if len(v) > 1:
                    for i in v:
                        index_li.append(result_li[i])
                    if "成功" in index_li:
                        for i in v:
                            result_li[i] = "成功"
            # 复制原文件
            f = open(file, encoding='gbk')
            # 1.newline=''消除空格行
            path01 = os.path.join(os.getcwd(), 'new_csv')
            if not os.path.exists(path01):
                os.mkdir(path01)
            path02 = os.path.join(path01, file)
            aim_file = open(path02, 'w', newline='', encoding='gbk')
            write = csv.writer(aim_file)
            reader = csv.reader(f)
            # for row in reader:
            #     row[6] = result_li[i]
            rows = [row for row in reader]
            # print(type(rows))
            print(rows)
            # 2.遍历rows列表
            x = 0
            for row in rows:
                try:
                    row[6] = result_li[x]
                    # 3.把每一行写到Aim.csv中
                    write.writerow(row)
                    x = x + 1
                except:
                    break

            f.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值