简体   繁体   English

尝试将 object 添加到文本文件中

[英]Trying to add object onto text file

I am trying to upload a bunch of objects onto a text file in an organized manner but I keep on getting an error.我正在尝试以有组织的方式将一堆对象上传到文本文件中,但我不断收到错误消息。 I am not sure about objects and how to arrange them so they appear in the text document.我不确定对象以及如何排列它们以便它们出现在文本文档中。

class Customer: 
    def __init__(self, name, date, address, hkid, acc):
        self.name = name
        self.date = date
        self.address = address
        self.hkid = hkid
        self.acc = acc

customer1 = Customer ("Sarah Parker","1/1/2000","Hong Kong, Tai Koo,Tai Koo Shing Block 22,Floor 10, Flat 1", "X1343434","2222")
customer2 = Customer ("Joe Momo","7/11/2002","Hong Kong, Tai Koo, Tai Koo Shing, Block 22, Floor 10, Flat 5", "C2327934","1234")
customer3 = Customer ("Brent Gamez","7/20/2002","Hong Kong, Tung Chung, Yun Tung, Block 33, Floor 10, Flat 9", "C1357434","2234")
customer4 = Customer ("Jose Gamez","7/20/2002","Hong Kong, Tung Chung, Yun Tung, Block 33, Floor 10, Flat 9", "C1357434","2234")
customer5 =Customer ("Viraj Ghuman","7/20/2002","Hong Kong, Heng Fa Chuen, 100 Shing Tai Road, Block 22, Floor 20, Flat 1", "C6969689","100000")
allCustom = [customer1, customer2, customer3, customer4, customer5]

def UpdateFile ():
    global allCustom
    OutFile = open("CustomInfo.txt","w")
    for i in range (len(allCustom)):
        for c in range (i):
            OutFile.write(allCustom[i["\n","Name:",c.name,"\n"]])
            OutFile.write(allCustom[i["Birth Date:",c.date,"\n"]])
            OutFile.write(allCustom[i["Address:",c.address,"\n"]])
            OutFile.write(allCustom[i["HKID:",c.hkid,"\n"]])
            OutFile.write(allCustom[i["Account value:", c.acc,"\n"]])
    OutFile.close()

You don't need two loops to get each object info.您不需要两个循环来获取每个 object 信息。 Maybe this is what you are looking for.也许这就是你要找的。

def UpdateFile():
    global allCustom
    Outfile = open("CustomInfo.txt", "w")
    
    for i in allCustom:
        Outfile.write(f'\nName: {i.name}\n')
        ...

    Outfile.close()

i and c are integer list indexes. ic是 integer 列表索引。 You can't use c.name because it's not a Customer object.您不能使用c.name ,因为它不是Customer object。 And you can't index i[...] because it's not a container.而且你不能索引i[...]因为它不是一个容器。

You don't need nested loops, just one loop over all the customers.您不需要嵌套循环,只需对所有客户进行一个循环。 Your loop iterates i from 0 to 4 .您的循环将i0迭代到4 On the first iteration it iterates 0 times, on the second iteration it processes c == 0 , on the third iteration it processes c == 0 and c == 1 , and so on.在第一次迭代中,它迭代 0 次,在第二次迭代中,它处理c == 0 ,在第三次迭代中,它处理c == 0c == 1

Then you can use a formatting operator to put the attributes into the strings that you're writing to the file (I've used f-strings below, but you can also use the % operator or the .format() method).然后,您可以使用格式化运算符将属性放入要写入文件的字符串中(我在下面使用了 f 字符串,但您也可以使用%运算符或.format()方法)。

def updateFile():
    global allCustom;
    with open("CustomInfo.txt", "w") as OutFile:
        for c in allCustom:
            OutFile.write(f"\nName:{c.name}\n")
            OutFile.write(f"Birth Date:{c.date}\n")
            OutFile.write(f"Address:{c.address}\n")
            OutFile.write(f"HKID:{c.hkid}\n")
            OutFile.write(f"Account value:{c.acc}\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM