Fernando
Fernando

Reputation: 429

Printing specific text from a line in python

I'm writing a mini diceware program designed to take inputs from the users with real dice, look up the values, and print out their diceware passphrase.

The code I have at the moment works fine and pulls the number and words from a wordlist by searching for the 5-digit diceware identifier (e.g. 34465 jilt).

But, I'm hoping to make the pass phrase print as one line without the number association. e.g. as

"jilt load re open snap" instead of

34465 jilt

load

etc.

At the moment this is that code I'm using:

p_length = int(raw_input("How many dicewords?"))
roll_list = []
for i in range(p_length):
    seq1 = (raw_input("roll 1:"), raw_input("roll 2:"),
            raw_input("roll 3:"), raw_input("roll 4:"),
            raw_input("roll 5:"))
    str1 = "".join(seq1)
    roll_list.append(str1)
    print roll_list

with open("name_list.txt") as f:
    for line in f:
        for x in roll_list:
            if x in line:
                print line

Any suggestions on how to change the last few lines there to do what I'm hoping?

Thanks for the split() advice. Here is my solution:

passphrases = []
with open("name_list.txt") as f:
    for line in f:
    for x in roll_list:
        if x in line:
            var1 = line.split(" ")
            var2 = var1.pop( )
            passphrases.append(var2.rstrip('\n'))

print " ".join(passphrases)

Upvotes: 0

Views: 80

Answers (2)

itzMEonTV
itzMEonTV

Reputation: 20339

You can use any here.Updated for just fixing.

for line in f:
    if any(i for i in roll_list if i in line.strip()):
        print line.strip().split(" ")[-1]

>>>hold romeo

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49803

You can use split to break up a line and extract just the word w/o the number.

Upvotes: 1

Related Questions