gpt4 book ai didi

python - 列表对象没有属性分割

转载 作者:太空宇宙 更新时间:2023-11-03 14:37:05 25 4
gpt4 key购买 nike

基本上就是标题所说的:我正在尝试创建一个程序来检测文件中的用户名和密码。但是,每当我运行它时,都会出现以下错误:

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

关于python - 列表对象没有属性分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46852711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com