How to change a specific line in a file, depending on chosen name?

后端 未结 3 1470
轻奢々
轻奢々 2021-01-21 22:41

I have a text file where i have numbers and names listed. each number belongs to a specific name, and they belong on the same line. the content of the file, looks like this:

3条回答
  •  爱一瞬间的悲伤
    2021-01-21 23:08

    It seems a dictionary would idealy suit the case. I would try this:

    names_dictionary = dict()
    f = open("hei.txt", "r")
    for name, number in zip(f, f):
        names_dictionary[name] = number
    

    to update any number simply:

    names_dictionary[name] = any_number
    

提交回复
热议问题