python - Python - 不会为员工开启或关闭时钟,也不会写入 csv 文件
问题描述
我的代码首先从文件中读取以确保用户名和密码正确。之后,它会要求用户输入他们是否想要开启或关闭时钟。用户下班后,应该将小时数保存到 CSV 文件中,但这并没有发生。我不能为此任务使用“导入 csv”。另外,这段代码是否适合时钟的开启和关闭,或者我应该改变什么?谢谢
role = input("Choose role [E]mployee / [P]ayroll clerk [Default]:")
if role == "E" or role == "e":
print("Please enter your details to log in:")
print()
user_name1 = input("Please enter your username: ")
pass_word1 = input("Please enter your password: ")
file = open("user_pass.csv", "r")
for row in file:
field = row.split(",")
user_name = field[0]
pass_word = field[1]
user_id = field[2]
# lastchar = len(pass_word) - 1
# pass_word = pass_word[0:lastchar]
if user_name1 == user_name and pass_word1 == pass_word:
print("Hello", user_name)
clock = input("Clock [I]n / Clock [O]ut:")
if clock == "I" or clock == "i":
date = input("Enter date: ")
time = input("Enter time: ")
print("Thanks for Clocking On.")
if clock == "O" or clock == "o":
date = input("Enter date: ")
time = input("Hours worked: ")
employee_data = open("CafeTestData.csv", "a+")
data = employee_data.readlines()
for line in data:
if line.split(",")[0] == user_id:
newline = line.split(",")
if date == "Monday":
newline[7] = time
if date == "Tuesday":
newline[8] = time
if date == "Wednesday":
newline[9] = time
if date == "Thursday":
newline[10] = time
if date == "Friday":
newline[11] = time
if date == "Saturday":
newline[12] = time
if date == "Sunday":
newline[13] = time
data.remove(line)
replacement = ""
for item in newline:
replacement = replacement + item + ","
replacement = replacement[0:len(replacement) - 1]
replacement += "\n"
data.append(replacement)
employee_data.writelines(data)
employee_data.close()
else:
print("Incorrect. Please try again.")
解决方案
推荐阅读
- python - 如何使用python脚本将没有标题的csv数据加载到cassandra列族中
- typescript - 将 typeof 分布在类的联合上
- node.js - MongoParseError:无效的消息大小:1347703880,允许的最大值:67108864(NestJS 和 MongoDB Docker 容器)
- xslt-2.0 - XSLT 合并具有不同名称空间的两个文件
- javascript - 我希望这个选择有一个对象值,这有效,但我无法显示所选对象的标题,有人知道为什么吗?
- paypal - PayPal Smart Button 交易在“批准”后不收费
- sql - 除以返回计数的行
- javascript - 如何修复快速应用程序中的渲染页面错误
- azure - 在 Logic App Select 中获取递增的数字
- service - 分析服务 - TMSL - 从 C# 执行