列表对象没有属性拆分
问题描述:
基本上标题说:我试图创建一个程序,检测文件中的用户名和密码。但是,每当我运行它,它想出了这个错误:列表对象没有属性拆分
Traceback (most recent call last):
File "C:/Users/tom11/Desktop/Data Login.py", line 33, in <module>
content = raw.split(",")
AttributeError: 'list' object has no attribute 'split'
下面是代码的地方是哪里错了:
UCheck = ""
PCheck = ""
Username = input("Username: ")
Attempts = 3
while UCheck != "Y":
lines = True
f = open('Data.txt', 'r+')
while lines:
raw = f.readlines()
content = raw.split(",")
if len(raw) == 0:
print("That Username does not exist!")
Username = input("Username: ")
elif Username == content[0]:
UCheck == "Y"
lines = False
这是什么.txt文件内:
TheCloudMiner,Password123
TestUser,TestPass
Testing,Tester
Username,Password
我已阅读了其他一些答案,但他们对我没有帮助。任何帮助将非常感激。
答
readlines()
返回字符串列表,而不是字符串。你想单独申请每行split()
,所以你应该遍历它的东西,如
for line in open(...).readlines():
username, password = line.split(",")
# rest of your code
'原料= f.readlines()'返回列表,而不是字符串。另外,看起来你正在寻找'f.readline()' –