python - Python 迭代混乱
问题描述
正如您在下面看到的,我正在尝试制作一个程序,该程序采用名为 names.txt 的文件,将其分解为带有 .split('/n') 的列表,然后为列表中的每个用户添加密码。出于某种原因, for 查找将列表视为字符串,因此不要将其视为:
鲍勃·查理·罗恩
它将它分解为:
鲍勃
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)
解决方案
你永远不会对 from 的返回值做任何事情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)
推荐阅读
- facebook-pixel - Facebook 像素不会在 PageView 上发送数据
- momentjs - 无法使用 react-moment Gatsby 解析“./locale”
- php - laravel 6 中的自定义邮件验证模板
- javascript - 在 Jquery 中创建父/子结构时寻求帮助
- python - 从 Flask Celery 调用 Google Speech to Text API 导致 celery 崩溃
- db2 - 未建立连接环境时调用语言接口
- typescript - 打字稿:使用扩展数组时初始化空数组
- r - 如何将频率表转换为趋势分析图
- android - 在 Android 中滚动“突出的顶部应用栏”的应用中的延迟滚动行为
- perl - 如何在 perl 中将散列推送或附加到多维散列上