How do I use lines from a text file as input in Python?

时间:2016-02-03 04:06:08

标签: python file input

Apologies if this is an incredibly simple question...I'm completely new to Python and am learning as I go.

An old post (Find all combinations (upper and lower and symbols) of a word in python) shows a way to provide multiple permutations of a input word into leet-speak (Thank you Moose!). The code works beautifully, but the code presented only allows one input word; in this case: Password.

I want to use a text file, with one word per line, as input into the code snippet shown in the link above and save the results into a new text file.

I would have thought it rather straightforward: open input file as read only, open output file for writing, substitute the value of infile.readlines() into the def and write result to the outfile. Rinse and repeat. Yet, despite trying a few different approaches and syntax, I can't get this to work.

My botched attempt to modify moose's code looks like this:

#!/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')

How do I get the string contained in each line of the file to be the input for getAllCombinations?

Thank you in advance for your help!

2 个答案:

答案 0 :(得分:0)

I'm guessing your wordlist_in.txt looks like this

word1
another_word
more_words

In that case, you only want to pass one word at a time to the function:

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

答案 1 :(得分:-1)

在接受了Patrick Carroll,pzp和Ben Graham的建议之后(感谢您的回复!我们肯定会看到您为改进代码而提出的一些选项。)并且做了一些试验和错误,我找到了我最初的尝试确实没有太大的错误,除了一个小问题之外,我至少自己做了一次。

我输入词汇表中的每个单词都在其自己的行中。当Python读取文件时,它包含了不可见的&#39; \ n&#39;控制字符作为单词的一部分,例如,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')

快乐编码,

Betawave