Change text in file with Python

前端 未结 4 518
忘了有多久
忘了有多久 2020-12-22 14:17
def false_to_true():
    name = input(\"Input name: \")
    file=open(\"users.txt\",\"r\")
    lines = file.readlines()
    file.close()
    for line in lines:
              


        
4条回答
  •  一个人的身影
    2020-12-22 14:57

    Open the input and output files, make a set out of the user-input names (terminated by a blank line), then create a generator for strings of the proper format that check for membership in the user-input names, then write these lines to the output file:

    with open('names.txt') as f, open('result.txt', 'w') as out:
        names = {name for name in iter(input, '')}
        f = ('{}/{}/{}'.format(a,b,'True\n' if a in names else c) for a,b,c in (line.split('/') for line in f))
        output.writelines(f)
    

提交回复
热议问题