【问题标题】:how to number each line of a file on python [duplicate]如何在python上对文件的每一行进行编号[重复]
【发布时间】:2017-01-18 15:54:48
【问题描述】:

我的任务是能够为用户提供给我的程序的每个输入生成一个唯一编号,并将此编号与他们的输入一起保存在我称为 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、案例 2 等)。如果这不是家庭作业和真实系统,那么我将使用数据库(sqlite 启动)。

标签: python enumerate


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多