Click here to Skip to main content
15,890,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code it currently only process and prints last line of input.txt file. I need it to process and print each line in text file.

Python
from cryptotools.BTC.HD import check, WORDS

with open("input.txt", "r") as a_file:
  for line in a_file:

    stripped_line = line.strip()    

    
for word in WORDS:        
    mnemonic = stripped_line.format(x=word)
    if check(mnemonic):
        print(mnemonic)
        
with open("print.txt", "a") as i:
            i.write(mnemonic)


Input file input.txt consist of
meat wrestle venue {x} act ostrich noise suggest happy violin nature ginger
riot execute army {x} wood journey harbor vessel critic human slim primary
easy famous update {x} magnet secret speak grab skirt tribe mushroom wrestle

What I have tried:

I have tried all types of .readlines() examples on python. Sorry, I am newbie with obsession to learn.
Posted
Updated 16-Aug-21 0:22am
Comments
Richard MacCutchan 16-Aug-21 6:16am    
I suspect that your indentation is not correct. But since you have not explained what the problem is I may be wrong.

1 solution

You need to indent your code properly. Your second for loop is currently outside of the first loop, and the final with/write block is outside of both. From your description, you need everything to execute within the first loop.
Python
with open("input.txt", "r") as a_file:
    for line in a_file:
        stripped_line = line.strip()
        for word in WORDS:
            mnemonic = stripped_line.format(x=word)
            if check(mnemonic):
                print(mnemonic)
                with open("print.txt", "a") as i:
                    i.write(mnemonic)
NB: If you want to write each mnemonic value on a new line in the output file, then you need to write a line separator after each one:
Python
...
                with open("print.txt", "a") as i:
                    i.write(mnemonic)
                    i.write('\n')
Built-in Functions — Python 3.9.6 documentation[^]
 
Share this answer
 
v2
Comments
OriginalGriff 16-Aug-21 6:49am    
Posted by OP as a new solution:
"You are champion! Thank you, one more question how do I write the results to the text file
one after the other! Thanks mate really helped me!"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900