微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将信息从数组读取和写入 txt 文件的问题

如何解决将信息从数组读取和写入 txt 文件的问题

我正在尝试为一个类编写一个程序,如下所示。我遇到的问题是输出

我希望文本文件的保存信息如下所示:

paul,shook,600

但是我为保存的文件得到了这个:

paul,600

['paul,600','','paul,smith,20']

我认为问题在于它的读取方式。

我还需要它在程序结束时显示此信息以及以编号项目符号保存的信息。但是,根据我如何更改列表功能,我可以在开始或结束时给我一个额外的项目符号空白项目符号。 这就是我得到的:

  1. paul,20
  2. paul,600
FILENAME = "payroll.txt"
    
def readFile():
    namePay = []
    with open (FILENAME)as file:
        for line in file:
            line = line.replace("\n","")
            namePay.append(str(line))
            print(line)
        return namePay
           
def appendFile(namePay):
    #with open (FILENAME,"a") as join
    join = open(FILENAME,"a")
    print(namePay,file = join,)
    join.close()
        
def listFile(namePay):
    for i in range(len(namePay)):
        emp = namePay[i-1]
        print(str(i+1) + ". " + emp)
    print()
   
def introduction():
    print("Python Assignment 6 by Paul Shook")
   
def gatherinformation(namePay):
    firstName = str(input("Enter Employee's First name: "))
    lastName = str(input("Enter Employess's Last name: "))
    weeklyPay = str(input("Enter Employess's Weekly pay: "))
    empInfo = []
    empInfo.append(firstName)
    empInfo.append(lastName)
    empInfo.append(weeklyPay)
    joined_list = ",".join(empInfo)
    namePay.append(joined_list)
    appendFile(str(namePay))
    print("You entered:" + firstName,lastName,weeklyPay + "\n")
    
def closing():
    print("Thanks for using payroll")
    input("Press the enterKey to end")
        
def main ():
    introduction()
    namePay = readFile()
    go = "yes"
    while go == "yes":
        gatherinformation(namePay)
        go = input("Enter 'yes' to continue,anything else to quit: ")
    else:
        listFile(namePay)
        closing()  
                
        #firstName = str(input("Enter Employee's First name: "))
        #lastName = float(input("Enter Employess's Last name: "))
        #weeklyPay = float(input("Enter EMployess's Weely pay: "))
        #namePay.append(
        #finalPay = calculatePay(payrate,hours)
        #namePay = (empName,finalPay)
        #runTotals.append(finalPay)
        #mylistofNames.append(namePay)
        #print("Weekly pay for:",*mylistofNames,sep = "\n")
        #print("Cumulative total is: ",(sum(runTotals)))
            
if __name__ == "__main__":
    main()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。