You need to enable JavaScript to run this app. Python开发员工打卡系统时CSV无法写入指定列反而覆盖整行求助
优惠活动
大模型
产品
解决方案
定价
更多

Python开发员工打卡系统时CSV无法写入指定列反而覆盖整行求助

代码问题修复方案

错误原因

  • 构造新行时仅拼接了工时字段,未保留原行前7列的原有数据,导致整行被覆盖
  • 删除原行后追加新行的逻辑会打乱原有文件的行顺序
  • 原有代码存在缩进错误、拼写错误(周日拼写为Sunder,应为Sunday)、布尔值误用字符串的小问题

修正后可运行代码

role = input("请选择角色 [E]员工 / [P]薪资专员 [默认]:")

if role == "E" or role == "e":
    logged_in = False
    # 读取账号密码文件
    with open("user_pass.csv", 'r', encoding='utf-8') as file:
        user_pass_list = []
        for row in file:
            field = row.strip().split(",")
            user_name = field[0]
            pass_word = field[1]
            user_id = field[2]
            user_pass_list.append([user_name, pass_word, user_id])
    
    # 登录逻辑
    while not logged_in:
        print("请输入登录信息:\n")
        user_name1 = input("请输入用户名: ")
        pass_word1 = input("请输入密码: ")
        for item in user_pass_list:
            if user_name1 == item[0] and pass_word1 == item[1]:
                logged_in = True
                user_name = item[0]
                user_id = item[2]
                break
        if not logged_in:
            print("账号密码错误,请重试。\n")
    
    print(f"你好,{user_name}")
    clock = input("打卡 [I]上班 / [O]下班:")
    
    if clock == "I" or clock == "i":
        completion = False
        # 读取打卡数据文件
        with open("CafeTestData.csv", "r", encoding='utf-8') as editfile:
            newfile = editfile.readlines()
        
        # 遍历匹配对应用户行
        for index, line in enumerate(newfile):
            line_fields = line.strip().split(",")
            if line_fields[0] == user_id:
                while not completion:
                    print(f"原有数据: {line}")
                    details = line.strip().split(",")
                    
                    mon_time = input("请输入周一工时: ")
                    mon_time = mon_time if mon_time else details[7]
                    
                    tue_time = input("请输入周二工时: ")
                    tue_time = tue_time if tue_time else details[8]
                    
                    wed_time = input("请输入周三工时: ")
                    wed_time = wed_time if wed_time else details[9]
                    
                    thu_time = input("请输入周四工时: ")
                    thu_time = thu_time if thu_time else details[10]
                    
                    fri_time = input("请输入周五工时: ")
                    fri_time = fri_time if fri_time else details[11]
                    
                    sat_time = input("请输入周六工时: ")
                    sat_time = sat_time if sat_time else details[12]
                    
                    sun_time = input("请输入周日工时: ")
                    sun_time = sun_time if sun_time else details[13]
                    
                    print(f"你输入的工时为: 周一{mon_time} 周二{tue_time} 周三{wed_time} 周四{thu_time} 周五{fri_time} 周六{sat_time} 周日{sun_time}")
                    confirmation = input("是否确认数据正确?[Y]是 / [N]否: ")
                    
                    if confirmation.lower() == "y":
                        # 核心修改:保留前7列原有内容,仅替换第7列及之后的工时数据
                        new_line_fields = details[:7] + [mon_time, tue_time, wed_time, thu_time, fri_time, sat_time, sun_time]
                        new_line = ",".join(new_line_fields) + "\n"
                        # 直接在原位置替换行,不打乱顺序
                        newfile[index] = new_line
                        completion = True
                        # 写入文件
                        with open("CafeTestData.csv", "w", encoding='utf-8') as editfile:
                            editfile.writelines(newfile)
                        print("工时已保存成功")
                        break
                    
                    if confirmation.lower() == "n":
                        print("请重新输入工时")
                break

核心修改说明

  1. 构造新行时使用details[:7]保留原行前7列的所有内容,仅替换后续的工时字段,不会覆盖原有基础信息
  2. 使用enumerate获取行索引,直接替换对应位置的行,不会打乱原文件的行顺序
  3. 用with open语法自动管理文件句柄,避免忘记关闭文件的问题
  4. 优化了登录匹配逻辑,匹配成功后直接break提升效率
  5. 所有交互提示翻译成中文,修正了周日的拼写错误

内容的提问来源于stack exchange,提问作者sss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 08:27:04