Python,如何检查一行的第一个元素是否在csv文档上重复



我在python中做一个简单的密码管理器,用户需要输入平台、用户和密码。

我试着填充csv,以防因为csv为空,我还将两个变量定义为列表。

我在尝试检查输入平台是否已经在csv文件中时不断收到错误。

这是代码的一部分:

if n=="1": 
fo=open(reqfile,'r')
data=csv.reader(fo)
datatoread=list(data)
while check==False:
newline.append(input("introduce platform:").lower)
for x in range(len(datatoread)-1):
if newline[0]==datatoread[x[0]]:
print("You already setup an username and password for this platform, please change the platform")

check=False
else:
check=True

在append语句之后打印(换行(时的输出:

[<0x037AF680>处str对象的内置方法下限]

这是我放置中断的地方:

fo=open(reqfile,'r')
data=csv.reader(fo)
datatoread=list(data)
while check==False:
newline.append(input("introduce platform:"))
for x in range(len(datatoread)):
print(newline)
print(datatoread)
if newline[0]==datatoread[x[0]]:
print("You already setup an username and password for this platform, please change the name of the platform")
check=False
newline.pop()
break
else:
check=True
break

输出为:

['youtube']

[[平台','用户名','密码']]

也有相同的错误

错误:

文件"PassMan.py";,第27行,in如果换行[0]==datatoread[x[0]]:TypeError:"int"对象不是可下标的

如果你需要所有的代码,这里有我的github repo:https://github.com/DavidGarciaRicou/PasswordManagerPython

我建议您使用panda来读写CSV文件。我相信它会帮助你处理这种文件类型。

这个答案显示了如何检查pandas数据帧(即CSV文件(中是否有字符串。

感谢大家的帮助。

我解决了这个代码的问题,仍然不知道我做错了什么:

while check==False:
newline.append(input("Introduce platform:"))

for x in range(0,len(datatoread)):
platcheck=datatoread[x]
plattochk=platcheck[0]
if newline[0]==plattochk:
print("You already setup an username and password for this platform, please change the name of the platform")
check=False
newline.pop()
break
try:
if newline[0]!=plattochk:
check=True
except:
pass
fo.close

最新更新