列表对象没有属性拆分

时间:2017-10-20 15:46:32

标签: python list attributes attributeerror

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

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

我已经阅读了其他一些答案,但对我没有帮助。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

readlines()返回字符串列表,而不是字符串。您希望分别在每一行应用split(),因此您应该使用类似

的内容进行迭代
for line in open(...).readlines():
    username, password = line.split(",")
    # rest of your code
相关问题