Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在文件中查找重复项_Python - Fatal编程技术网

Python 如何在文件中查找重复项

Python 如何在文件中查找重复项,python,Python,基本上,我想尝试建立一个系统,当你输入你的身份证号码,然后用相同的号码再次输入时,它会显示一个错误。我试着在网上寻找一些解决方案,但似乎找不到一个真正可行的。这是我到目前为止所做的代码: import sys N = 0 while N < 2: ID = input("Please input ID code ") if (len(ID)) == 6: with open('ID1.txt', 'a') as file: file.

基本上,我想尝试建立一个系统,当你输入你的身份证号码,然后用相同的号码再次输入时,它会显示一个错误。我试着在网上寻找一些解决方案,但似乎找不到一个真正可行的。这是我到目前为止所做的代码:

import sys
N = 0
while N < 2:
    ID = input("Please input ID code ")
    if (len(ID)) == 6:
        with open('ID1.txt', 'a') as file:
            file.write(ID + ' ')
            file.write('\n')
        N += 1
        print("ID length: Valid")
    else:
        print("ID Code: Error")
        sys.exit()
导入系统 N=0 当N<2时: ID=输入(“请输入ID代码”) 如果(len(ID))==6: 以open('ID1.txt','a')作为文件: file.write(ID+“”) file.write(“\n”) N+=1 打印(“ID长度:有效”) 其他: 打印(“ID代码:错误”) sys.exit()
有人知道怎么做吗?

使用您的代码,最简单的方法是加载文本文件,通过换行将其拆分为ID,然后查看ID是否在此新列表中:

N = 1
while N <= 2:
    input_id = input('Type your ID: ')

    assert input_id == 6, 'Invalid ID length'

    with open('IDs.txt', 'r+') as ids:
        id_list = ids.read().split('\n')
        if input_id in id_list:
            print('This ID already exists')
            N += 1
            continue
        else:
            ids.write('\n'+input_id)
            print('valid ID')
            break
N=1

N使用代码,最简单的方法是加载文本文件,通过换行符将其拆分为ID,然后查看ID是否在这个新列表中:

N = 1
while N <= 2:
    input_id = input('Type your ID: ')

    assert input_id == 6, 'Invalid ID length'

    with open('IDs.txt', 'r+') as ids:
        id_list = ids.read().split('\n')
        if input_id in id_list:
            print('This ID already exists')
            N += 1
            continue
        else:
            ids.write('\n'+input_id)
            print('valid ID')
            break
N=1

下面的代码验证文件中是否存在ID,如果不存在,则在文件中输入2个唯一ID

N = 0
while N < 2:
    ID = raw_input("Please input ID code ")
    if (len(ID)) == 6:
        with open('ID1.txt', 'a+') as f:
            if not any(ID == x.rstrip('\r\n') for x in f):
                f.write(ID + '\n')
                N += 1
N=0
当N<2时:
ID=原始输入(“请输入ID代码”)
如果(len(ID))==6:
以open('ID1.txt','a+')作为f:
如果不存在(对于f中的x,ID==x.rstrip('\r\n'):
f、 写入(ID+“\n”)
N+=1

下面的代码验证该ID是否存在于文件中。如果不存在,请在文件中输入两个唯一的ID

N = 0
while N < 2:
    ID = raw_input("Please input ID code ")
    if (len(ID)) == 6:
        with open('ID1.txt', 'a+') as f:
            if not any(ID == x.rstrip('\r\n') for x in f):
                f.write(ID + '\n')
                N += 1
N=0
当N<2时:
ID=原始输入(“请输入ID代码”)
如果(len(ID))==6:
以open('ID1.txt','a+')作为f:
如果不存在(对于f中的x,ID==x.rstrip('\r\n'):
f、 写入(ID+“\n”)
N+=1

您必须存储已使用的ID。例如在列表中。然后检查它是否已经在列表中。如果第二次键入的id等于第一次键入的id,或者键入的id是否包含在文件中,则要引发错误?可以使用集合来存储id。对于少量ID,访问时间没有明显差异,但假设您有数百万个ID。设置会很快,您必须存储已使用的ID。例如在列表中。然后检查它是否已经在列表中。如果第二次键入的id等于第一次键入的id,或者键入的id是否包含在文件中,则要引发错误?可以使用集合来存储id。对于少量ID,访问时间没有明显差异,但假设您有数百万个ID。布景会更快