Python Iteration Mess

问题描述 投票:0回答:2

正如您在下面看到的,我正在尝试创建一个程序,该程序采用名为names.txt的文件,将其分解为带有.split('/ n')的列表,然后为列表中的每个用户添加密码。出于某种原因,for look将列表视为字符串,因此不要将其视为:

鲍勃查理罗恩

它打破了它:

B和b

C h ....因此给每个字母一个密码而不是列表本身。

我究竟做错了什么?有没有办法我可以测试看看for循环实际上是什么?

我一直在玩它以查看它是否被赋予了字符串,但是列表在打印时似乎是一个清单,所以为什么它会像for循环中的字符串一样对待?

def importNames():

    try:
        file = open("names.txt", "r")
        contents = file.read()
        print(contents)
        file.close()

        return contents

    except:
        print("The file was not found.")
        print("Make sure you have a file called \"names.txt\" in the working directory (where you ran this program)")

        exit()

def createPasswordList(nameList):
    print("They are being given the standard password of \"p@ssw0rd.\"")
    #nameList = ["Pickles", "Bob's", 'wow zers john'] #This is for testing purposes. It works just fine, but the original nameList breaks down badly. 
    passList = []

    #for x, value in enumerate(nameList):
    for i in nameList:
        print(i)
        passList.append("p@ssw0rd")

    print(passList)
    return passList

def createUsers(nameList, passwordList): #takes in a list of usernames, then adds creates them. 
    for i in nameList:
        print("This will do something soon.")
        #encPass = crypt.crypt(passwordList[i],"22") #22 is a salt number, use crypt per useradd manual
        #os.system("useradd -p " + encPass + " " + nameList[i]) #useradd -p encryptedpass username

def convertNamesToList(originalList):
    print("Converting names.") #Use newline as a delimiter
    newList = originalList.split('\n')
    print(newList)
    return newList

def convertNameToUsernames(originalNames):
    print("Creating usernames")

print("This program is used to make some users.")
print("If you import a file, have it in the working directory called \"names.txt\".")

print("Would you like to import names or add them manually? (n/m)")
answer = input()

if (answer is 'm' or answer is 'M'):
    createUser()
    print("The user gets created. Good job.")

else:
    originalNames = importNames() #this will return a string of the file.
    convertNamesToList(originalNames) #convert the string to sets of names. Use newlines as delimiters. 
    #convert the names to usernames

    print("Do the users have passwords? (y/n)")
    #answer = input()
    answer = 'n' ###FORCE###
    if (answer is 'n' or answer is 'N'):
        passwordList = createPasswordList(originalNames)


python
2个回答
2
投票

你永远不会用convertNamesToList的返回值做任何事情。你不会把它存放在任何地方。然后你传递originalNames,这是一个字符串(你甚至在评论中指出这一点)给createPasswordList

我想你想要的东西:

originalNames = importNames() #this will return a string of the file.
listOfNames = convertNamesToList(originalNames) #convert the string to sets of names. Use newlines as delimiters. 
#convert the names to usernames

print("Do the users have passwords? (y/n)")
#answer = input()
answer = 'n' ###FORCE###
if (answer is 'n' or answer is 'N'):
    passwordList = createPasswordList(listOfNames)

0
投票

看起来你的nameList实际上是一个字符串。假设names.txt文件的每一行都有一个名称,试试这个:

def importNames():
    try:
        file = open("names.txt", "r")
        contents=[]
        for line in file.read():
            contents.append(line)
        print(contents)
        file.close()
        return contents

然后,您不需要将其转换为列表。

© www.soinside.com 2019 - 2024. All rights reserved.