【问题标题】:How do I use lines from a text file as input in Python?如何使用文本文件中的行作为 Python 中的输入?
【发布时间】:2016-02-03 04:06:08
【问题描述】:

抱歉,如果这是一个非常简单的问题...我对 Python 完全陌生,并且正在学习。

一篇旧帖子 (Find all combinations (upper and lower and symbols) of a word in python) 展示了一种将输入单词的多种排列方式提供给 leet-speak 的方法(谢谢 Moose!)。代码运行得很漂亮,但呈现的代码只允许输入一个单词;在这种情况下:密码。

我想使用一个文本文件,每行一个单词,作为上面链接中显示的代码 sn-p 的输入,并将结果保存到一个新的文本文件中。

我认为这很简单:以只读方式打开输入文件,以写入方式打开输出文件,将 infile.readlines() 的值替换为 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 的输入?

提前感谢您的帮助!

【问题讨论】:

  • 您的 wordlist_in.txt 文件是什么样的?
  • 你的最终目标是什么?为什么需要输出所有的组合?也许有更好的方法来实现这一目标。
  • 我正在使用输出来创建大量的词表,这些词表又被用作哈希破解的来源。我尽可能使用规则来充分利用 GPGPU 性能,但有时您只需要一些好的单词列表作为来源,尤其是对于组合攻击。

标签: python file input


【解决方案1】:

我猜你的 wordlist_in.txt 看起来像这样

word1
another_word
more_words

在这种情况下,您一次只想向函数传递一个单词:

data = infile.readlines()
for line in data:
    for el in getAllCombinations(line):
        outfile.write(el+'\n')

【讨论】:

  • 您对单词表格式的看法是正确的。我曾考虑过“for line in data:”这一行,但这会引发以下错误: Traceback (last most recent call last): File "l33t.pl", line 20, in for el in getAllCombinations(line) :文件“l33t.pl”,第 14 行,在 getAllCombinations 中用于产品中的字母(*getPlaces(密码)):文件“l33t.pl”,第 11 行,在 getPlaces = lambda 密码:[leet[ord(el .upper()) - 65] for el in password] IndexError: list index out of range
  • 最好还是使用for line in infile:。文件指针也是可迭代的。您还可以使用outfile.write('\n'.join(getAllCombinations(line))) 来节省多个(昂贵的)写入操作。
  • 写入被缓冲,所以它不会像第一眼看上去那么糟糕。此外,在处理文件输入时,请查看docs.python.org/3.5/library/fileinput.html——它允许您的脚本使用标准输入,例如combinations.py &lt; words.txt
  • 是的,可以...我可以尝试...但我想知道我是否会遇到内存问题,因为文件变得非常大,非常快(几句话可以最终会达到许多 GB)。
  • @BenGraham 好电话,你说得对。但第一条评论仍然有效。
【解决方案2】:

在听取了 Patrick Carroll、pzp 和 Ben Graham 的建议(感谢您的回复!我一定会看看您为改进代码而提出的一些选项。)并进行了一些试验和错误,我发现那里我最初的尝试确实没有太大问题,除了一个小问题外,我至少自己做对了一次。

我的输入单词列表中的每个单词都在自己的行中。当 Python 读取文件时,它包含不可见的 '\n' 控制字符作为单词的一部分,例如,alpha 变为 alpha\n。这个控制字符吓坏了 Python 函数。通过插入新行并使用 rstrip(),我能够纠正问题并且代码运行完美。

这是按预期工作的最终修改代码(原始代码的所有功劳归功于@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 line in data:
        line=line.rstrip('\n')
        for el in getAllCombinations(data):
            outfile.write(el+'\n')

编码愉快,

贝塔波

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多