我目前正在为 Barbershop POS 实施使用漂亮的表格,程序需要生成管理员用户可以查看的报告。每次用户记录从 csv 执行的服务时,报告都会从 csv 中逐行复制(转换为漂亮的表格)。
我希望将这些行写入一个新的报告 csv 文件,并由管理员通过 python 解释器和其他文件系统访问。
到目前为止,程序从原始 csv 复制行(它正在打印出来),但没有将它们写入报告日志 csv。
有谁知道如何实现这个?
这是我的代码:使用服务打开 csv 并将其转换为表格的模块:
from prettytable import from_csv
def servicetable():
fp = open("Barbershop.csv", "r")
file = from_csv(fp)
fp.close()
print(file)
复制该行并将其粘贴到 log.csv 中的函数
def log_service():
while True:
try:
response3 = input("Please choose service from list using the ServiceID.\n")
response3 = int(response3)
#copy service table into another table
fp = open("Barbershop.csv", "r")
file_copy = from_csv(fp) #filecopy is a variable that holds the table
fp.close()
x = file_copy[response3:(response3+1)] #copy this row of the table
print(x) #print it out as confirmation
y = open("log.csv", 'wb') #create and open another csv file
writer = csv.writer(y, delimiter=' ',quotechar='"',quoting= csv.QUOTE_NONE) #create writer object
writer.writerows(y) #write each iteration value of x into log.csv
choice2 = input("Continue yes/ no:").lower()
if not(choice2=="yes" or choice2=="y"):
break
except ValueError:
print("Please enter valid ID")
当然,也欢迎对该概念进行更简单或更有效的实现。一如既往,非常感谢!