如何在python上编号文件的每一行

时间:2017-01-18 15:54:48

标签: python enumerate

我的任务是能够为用户提供程序的每个输入生成一个唯一的数字,并将这个数字及其输入保存在我称为customer_references的文件中。 我希望文件提供这样的内容:

案件编号:1 问题:我的问题是......

案件编号:2 问题:我的手机是......

案件编号:3 问题: ...... (等)

还需要能够打印用户在python上知道的案例编号。 我尝试使用randint,但这不够有效,因为案例编号有可能为不同的用户重复

这是我未完成的代码:

def issue_details():

    #prints aplogy message for not being able to find a solution
    print("Sorry we were unable to provide a suitable solution")

    #Takes further information on details of issue from the user
    issue = input("Please explain your issue in as much detail so we can direct you to a technician: ")


    #opens the file I created to store the customers details to append the issue into it.
    customer_file = open("Customer_references.txt","a")

    #inputs the issue into the text file
    customer_file.write(issue)

    #Here I need to generate the case number and add it to my file beside the users issue
    #The case number must always be unique

    print("Thank you for your cooperation. Contact us on 0844558888 and give the technician your reference number so we can try help you\n", "Your reference is: ",#case_number )
    customer_file.close()

1 个答案:

答案 0 :(得分:1)

randint对于唯一ID而言不够大。您应该使用uuid模块生成一个UUID号码,虽然是随机的,但可以安全地被认为是唯一的,用于所有实际目的