在Python中使用Pretty Table从表中复制行并将其写入csv文件

时间:2016-04-06 13:09:32

标签: python csv python-3.x prettytable

我目前正在使用漂亮的桌子用于理发店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")

当然,也欢迎更简单或更有效的概念实施。 一如既往,非常感谢!

0 个答案:

没有答案
相关问题