如何修复.txt导入?

京吾

我正在学习Python。刚拿起学习词典。这是导出到.txt在此处输入图片说明

import uuid  # GET A RANDOM ID FOR THE CUSTOMER
from datetime import date  # GET CURRENT DATE
from csv import DictWriter

file = open('CustomerNames.txt', 'w')
file1 = open('Orders_Per_Users.txt', 'a')
file2 = open('data_entered.csv', 'a')
x = -1
in_list = -1
length = 0
Total_Amount = 0.0
Customer_List = []
Address_List = []
sec_Customer_List = []
Today_Key = date.toordinal(date.today())
Today_Date = date.today()
Print_Today = Today_Date
Customers = {}
Dates = {}
FirstEmployeeAccountUsername = "coffee1"
FirstEmployeeAccountPassword = "coffeeshop1"
SecondEmployeeAccountUsername = "coffee2"
SecondEmployeeAccountPassword = "coffeeshop2"
ThirdEmployeeAccountUsername = "coffee3"
ThirdEmployeeAccountPassword = "coffeeshop3"

print("Welcome to our coffee shop!")
print("Login")

# EMPLOYEE LOGIN PROCESS STARTS
LoginEnter = True
while LoginEnter:
    username = input("Username: ")
    password = input("Password: ")
    if username == FirstEmployeeAccountUsername and password == FirstEmployeeAccountPassword or username == SecondEmployeeAccountUsername and password == SecondEmployeeAccountPassword or username == ThirdEmployeeAccountUsername and password == ThirdEmployeeAccountPassword:
        print("Login Successful")
        LoginEnter = False
    else:
        print("Invalid Login. Try again")
# EMPLOYEE LOGIN PROCESS ENDS


# PROCESS AFTER ORDER PLACEMENT STARTS
process1 = True
process2 = True
while process1:
    while process2:
        x += 1

        Customer_Name = input("Customer's Name:")
        Customer_Address = input("Customer's Address:")
        Address_List.append(Customer_Address)
        Customer_List.append(Customer_Name)
        sec_Customer_List.append(Customer_Name)


        if x == 1:
            if Customer_Address in Address_List:
                First_Index = Address_List.index(Customer_Address)
                if Customer_Name == Customer_List[First_Index]:
                    Customer_List.pop(First_Index)
                    Address_List.pop(First_Index)

                    x = x - 1

        if Today_Key not in Dates:
            Dates[Today_Key] = {}
            if Customer_Name not in Dates[Today_Key]:
                Dates[Today_Key][Customer_Name] = 1
            else:
                Dates[Today_Key][Customer_Name] += 1

        if Customer_Name in Customers:
            Customers[Customer_Name]['Orders'] += 1
            Customers[Customer_Name]['TotalAmount'] = Total_Amount
        else:
            Customers[Customer_Name] = {}
            Customers[Customer_Name]['Name'] = Customer_Name
            Customers[Customer_Name]['Address'] = Customer_Address
            Customers[Customer_Name]['ID'] = uuid.uuid1()
            Customers[Customer_Name]['Orders'] = 1
            Customers[Customer_Name]['TotalAmount'] = 0


        print(Customer_Name, "has ordered {} time(s)".format(Customers[Customer_Name]['Orders']))
        if Customers[Customer_Name]['TotalAmount'] == 0:
            print("This is the first time", Customer_Name, "orders")
        else:
            print(Customer_Name, "has spent", Customers[Customer_Name]['TotalAmount'], "in total")

        print("Current Date is: {}".format(Today_Date))
        Order_Price = float(input("Total amount of order:"))
        Total_Amount = Order_Price + Total_Amount
        if Print_Today != Today_Date:
            print("Total amount of orders today is: ", float(Total_Amount))
        answer1 = input("Send another order? (Y/N)").lower()
        process2 = answer1 == "y"
    LengthCustomersList = len(Customer_List)
    length += 1
    in_list += 1

    file.write(str(sec_Customer_List[0:]) + '\n')  # TAKE CARE FOR DUPLICATE NAMES FROM SAME ADDRESS and \n not working

    file1.write(Customer_List[x] + " has ordered " + str(Customers[Customer_Name]['Orders']) + " times in total\n")
    # FIX DUPLICATES SAME NAME SAME ADDRESS

    csv_writer = DictWriter(open('data_entered.csv', 'a'),
                            fieldnames=['Customer Name', 'Customer Address', 'Customer ID', 'Total Orders',
                                        'Total Amount'])
    csv_writer.writeheader()
    for customer_name in Customers.keys():
        csv_writer.writerows(
            [{'Customer Name': Customers[customer_name]['Name'], 'Customer Address': Customers[customer_name]['Address'],
              'Customer ID': Customers[customer_name]['ID'],
              'Total Orders': Customers[customer_name]['Orders'],
              'Total Amount': Customers[customer_name]['TotalAmount']}])  # TOTAL AMOUNT= 0 IF NOT MORE THAN 2 TIMES ORDER

    if int(length) == int(LengthCustomersList):
        process1 = False
file.close()
file1.close()
file2.close()

这是我目前得到的输出

输出量

我的问题是为什么在这种特殊情况下,为什么只在.txt文件中看到Mike而没有看到Michelle?

* PS。我想亲自了解解决方案,因此可以提供一个解释(如果您发送解决方案)。

哈沙娜·塞拉辛格(Harshana Serasinghe)

file.write在第二个循环之外调用该函数。因此,它只写入一次文本文件。为了解决此问题,请file.write在第二个循环内添加函数。

码:

while process1:
    while process2:
        x += 1

        Customer_Name = input("Customer's Name:")
        Customer_Address = input("Customer's Address:")
        Address_List.append(Customer_Address)
        Customer_List.append(Customer_Name)
        if x == 1:
            if Customer_Address in Address_List:
                First_Index = Address_List.index(Customer_Address)
                if Customer_Name == Customer_List[First_Index]:
                    Customer_List.pop(First_Index)
                    Address_List.pop(First_Index)

                    x = x - 1

        if Today_Key not in Dates:
            Dates[Today_Key] = {}
            if Customer_Name not in Dates[Today_Key]:
                Dates[Today_Key][Customer_Name] = 1
            else:
                Dates[Today_Key][Customer_Name] += 1

        if Customer_Name in Customers:
            Customers[Customer_Name]['Orders'] += 1
            Customers[Customer_Name]['TotalAmount'] = Total_Amount
        else:
            Customers[Customer_Name] = {}
            Customers[Customer_Name]['Name'] = Customer_Name
            Customers[Customer_Name]['Address'] = Customer_Address
            Customers[Customer_Name]['ID'] = uuid.uuid1()
            Customers[Customer_Name]['Orders'] = 1
            Customers[Customer_Name]['TotalAmount'] = 0

        print(Customer_Name, "has ordered {} time(s)".format(Customers[Customer_Name]['Orders']))
        if Customers[Customer_Name]['TotalAmount'] == 0:
            print("This is the first time", Customer_Name, "orders")
        else:
            print(Customer_Name, "has spent", Customers[Customer_Name]['TotalAmount'], "in total")

        print("Current Date is: {}".format(Today_Date))
        Order_Price = float(input("Total amount of order:"))
        Total_Amount = Order_Price + Total_Amount
        if Print_Today != Today_Date:
            print("Total amount of orders today is: ", float(Total_Amount))
        answer1 = input("Send another order? (Y/N)").lower()
        process2 = answer1 == "y"
        file.write(str(Customer_List[0:]) + '\n')  # TAKE CARE FOR DUPLICATE NAMES FROM SAME ADDRESS

        file1.write(Customer_Name + " has ordered " + str(Customers[Customer_Name]['Orders']) + " times in total\n")
        # FIX DUPLICATES SAME NAME SAME ADDRESS

    LengthCustomersList = len(Customer_List)
    length += 1
    in_list += 1



    csv_writer = DictWriter(open('data_entered.csv', 'a'),
                            fieldnames=['Customer Name', 'Customer Address', 'Customer ID', 'Total Orders',
                                        'Total Amount'])
    csv_writer.writeheader()
    for customer_name in Customers.keys():
        csv_writer.writerows(
            [{'Customer Name': Customers[customer_name]['Name'], 'Customer Address': Customers[customer_name]['Address'],
              'Customer ID': Customers[customer_name]['ID'],
              'Total Orders': Customers[customer_name]['Orders'],
              'Total Amount': Customers[customer_name]['TotalAmount']}])  # TOTAL AMOUNT= 0 IF NOT MORE THAN 2 TIMES ORDER

    if int(length) == int(LengthCustomersList):
        process1 = False
file.close()
file1.close()
file2.close()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何修复.csv导入?

来自分类Dev

如何修复Python 3.7导入错误

来自分类Dev

如何使用python 3.5将mysql数据导入.txt文件?

来自分类Dev

如何修复石墨中的守护程序导入错误?

来自分类Dev

如何修复jenkins中的python模块导入错误

来自分类Dev

如何修复仅允许作为顶级任务的Ant导入?

来自分类Dev

如何在PyCharm上修复Anaconda的导入错误?

来自分类Dev

如何修复“错误:遇到无法恢复的循环解析导入”?

来自分类Dev

如何自动在Rust中修复未使用的导入?

来自分类Dev

如何修复“ SyntaxError:无法在模块外部使用导入语句”

来自分类Dev

如何修复“导入com.dyuproject无法解析”错误

来自分类Dev

如何修复jenkins中的python模块导入错误

来自分类Dev

如何修复“未捕获的语法错误:意外的令牌导入”?

来自分类Dev

如何在 Eclipse 中修复未解析的导入 PyDev

来自分类Dev

如何修复“导入错误:DLL 加载失败:找不到指定的过程”。在导入 tensorflow-gpu 时?

来自分类Dev

如何将数据从txt文件导入到MySQL数据库

来自分类Dev

如何在R中导入多个txt文件并添加新标题?

来自分类Dev

如何使用诗歌从现有项目中导入requirements.txt

来自分类Dev

.txt文件的内容如何导入/复制到Web文本框中?

来自分类Dev

如何将txt文件中的3D点坐标导入python中的pymeshlab

来自分类Dev

如何从netscape cookies.txt文件导入cookie并使用它们?

来自分类Dev

如何将.txt文件中的“高级格式”滑块问题导入到Qualtrics中?

来自分类Dev

如何将此txt文件导入SqlLiteStudio中的数据库表

来自分类Dev

如何将UTF-8编码的txt文件导入jsp?

来自分类Dev

如何导入没有分隔符和逗号表示缺失值的 csv 或 txt 文件

来自分类Dev

如何将 .txt 文件中的数据导入 python 中的数组

来自分类Dev

如何从通过导入 .txt 文件制作的列表中选择随机作品?

来自分类Dev

如何修复由于缺少“ gi.repository”而导致的“无法导入:GTK +”错误?

来自分类Dev

如何在不使用SQLAlchemy的蓝图MySQL中修复Flask项目中的循环导入?

Related 相关文章

  1. 1

    如何修复.csv导入?

  2. 2

    如何修复Python 3.7导入错误

  3. 3

    如何使用python 3.5将mysql数据导入.txt文件?

  4. 4

    如何修复石墨中的守护程序导入错误?

  5. 5

    如何修复jenkins中的python模块导入错误

  6. 6

    如何修复仅允许作为顶级任务的Ant导入?

  7. 7

    如何在PyCharm上修复Anaconda的导入错误?

  8. 8

    如何修复“错误:遇到无法恢复的循环解析导入”?

  9. 9

    如何自动在Rust中修复未使用的导入?

  10. 10

    如何修复“ SyntaxError:无法在模块外部使用导入语句”

  11. 11

    如何修复“导入com.dyuproject无法解析”错误

  12. 12

    如何修复jenkins中的python模块导入错误

  13. 13

    如何修复“未捕获的语法错误:意外的令牌导入”?

  14. 14

    如何在 Eclipse 中修复未解析的导入 PyDev

  15. 15

    如何修复“导入错误:DLL 加载失败:找不到指定的过程”。在导入 tensorflow-gpu 时?

  16. 16

    如何将数据从txt文件导入到MySQL数据库

  17. 17

    如何在R中导入多个txt文件并添加新标题?

  18. 18

    如何使用诗歌从现有项目中导入requirements.txt

  19. 19

    .txt文件的内容如何导入/复制到Web文本框中?

  20. 20

    如何将txt文件中的3D点坐标导入python中的pymeshlab

  21. 21

    如何从netscape cookies.txt文件导入cookie并使用它们?

  22. 22

    如何将.txt文件中的“高级格式”滑块问题导入到Qualtrics中?

  23. 23

    如何将此txt文件导入SqlLiteStudio中的数据库表

  24. 24

    如何将UTF-8编码的txt文件导入jsp?

  25. 25

    如何导入没有分隔符和逗号表示缺失值的 csv 或 txt 文件

  26. 26

    如何将 .txt 文件中的数据导入 python 中的数组

  27. 27

    如何从通过导入 .txt 文件制作的列表中选择随机作品?

  28. 28

    如何修复由于缺少“ gi.repository”而导致的“无法导入:GTK +”错误?

  29. 29

    如何在不使用SQLAlchemy的蓝图MySQL中修复Flask项目中的循环导入?

热门标签

归档