列表 Object 没有属性拆分
List Object has no Attribute Split
基本上就是标题所说的:我正在尝试创建一个程序来检测文件中的用户名和密码。但是,每当我 运行 它时,它都会出现此错误:
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()
returns 字符串列表,而不是字符串。你想在每一行上分别应用 split()
,所以你应该用类似
的东西迭代它
for line in open(...).readlines():
username, password = line.split(",")
# rest of your code
基本上就是标题所说的:我正在尝试创建一个程序来检测文件中的用户名和密码。但是,每当我 运行 它时,它都会出现此错误:
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()
returns 字符串列表,而不是字符串。你想在每一行上分别应用 split()
,所以你应该用类似
for line in open(...).readlines():
username, password = line.split(",")
# rest of your code