如果这是一个非常简单的问题,我很抱歉…我对Python完全陌生,并在不断学习。在
一篇旧文章(Find all combinations (upper and lower and symbols) of a word in python)展示了一种将一个输入词进行多重排列的方法。代码运行得很好,但是显示的代码只允许输入一个单词;在本例中是Password。在
我想使用一个文本文件,每行一个单词,作为上面链接中显示的代码段的输入,并将结果保存到一个新的文本文件中。在
我本以为这很简单:以只读方式打开输入文件,打开输出文件进行写入,用填充读取线()写入def并将结果写入输出文件。冲洗并重复。然而,尽管尝试了一些不同的方法和语法,我还是不能让它发挥作用。在
我修改moose代码的拙劣尝试如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from itertools import product
def getAllCombinations(password):
leet = ["Aa@","Bb","Cc", "Dd","Ee","Ff","Gg","Hh","Ii","Jj","Kk",
"Ll","Mm","Nn","Oo0","Pp","Qq","Rr","Ss5","Tt","Uu","Vv",
"Ww","Xx","Yy","Zz"]
getPlaces = lambda password: [leet[ord(el.upper()) - 65] for el in password]
for letters in product(*getPlaces(password)):
yield "".join(letters)
with open("wordlist_in.txt", "r") as infile, open("wordlist_out.txt", "w") as outfile:
data = infile.readlines()
for el in getAllCombinations(data): <<<Pretty sure this is where I go wrong
outfile.write(el+'\n')
如何获取文件每行中包含的字符串作为getAllCombinations的输入?在
提前感谢您的帮助!在
在听取了帕特里克·卡罗尔、pzp和本·格雷厄姆的建议后(谢谢你的回复!)!!我肯定会看看你提出的一些改进代码的方法。)经过反复试验,我发现我最初的尝试并没有太大的错误,我自己至少有一次成功了,除了一个小问题。在
我输入单词表中的每个单词都在自己的行上。当Python读取文件时,它将不可见的'\n'控制字符作为单词的一部分,例如alpha变成alpha\n。这个控制字符使Python函数异常。通过插入一行新行并使用rstrip(),我能够纠正问题,代码运行得非常完美。在
以下是最终修改后的代码(原始代码全部归功于@moose):
编码愉快
隐居
我猜你的单词表_英寸.txt看起来像这样
在这种情况下,一次只需要向函数传递一个字:
^{pr2}$相关问题 更多 >
编程相关推荐