我目前正在使用漂亮的桌子用于理发店P.O.S实施,其中程序需要生成管理员用户可以查看的报告。每次用户记录已执行csv的服务时,都会从csv(转换为漂亮的表)逐行复制报告。
我希望将这些行写入新的报告csv文件,并由管理员通过python解释器和其他文件系统进行访问。
到目前为止,程序会复制原始csv中的行(它将它们打印出来),但不会将它们写入报告日志csv。
有谁知道如何实现这个?
这是我的代码: 使用Services打开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")
当然,也欢迎更简单或更有效的概念实施。 一如既往,非常感谢!