How to make the "word" variable take the lines from a text file automatically? After processing the first line, the result is output, written to a file, and then everything repeats with the second, third, etc. line.

__author__ = 'xcbtrader' # -*- coding: utf-8 -*- from bitcoin import * def crear_addr_word(word): priv = sha256(word) pub = privtopub(priv) addr = pubtoaddr(pub) wif = encode_privkey(priv, 'wif') return addr, priv, wif word = input('Ведите фразу') addr, priv, wif = crear_addr_word(word) print('####################################################') print('WORD: ' + word) print('ADDR: ' + addr) print('PRIV: ' + priv) print('WIF: ' + wif) print('####################################################') 
  • coding: utf-8 better at the very top (after the #! -stars if any) to indicate. Although on Python 3 this line can be omitted altogether (utf-8 is the default value for encoding the source code) - jfs
  • @jfs, just need to understand what to copy github.com/xcbtrader/bitcoin-address/blob/master/addr-word.py :) - gil9red

2 answers 2

 with open('фразы.txt') as file: for line in file: word = line.strip() if not word or word.startswith('#'): # skip blank lines and comments continue addr, priv, wif = crear_addr_word(word) ... 

This code assumes that the text in the file is encoded using locale.getpreferredencoding(False) (for example, cp1251 on Russian Windows or utf-8 on other desktop systems). If not, then explicitly pass the encoding argument.

  • In your example, the script processes only the most recent line from the file and stops. - Yuri
  • I just deleted: word = input ('Enter the phrase') and replaced it with your version. - Yuri
  • the code reads every line, except for empty and comments (start with # ). Take the input file of two (no more lines) and replace in my code the line with crear_addr_word with just print(word) . Dots (and further if your code goes) erase. If this code prints only the last line, then update the question and add the code with print() and the corresponding input file - jfs
  • The script printed both lines. - Yuri
  • It turns out my script sees all the lines, but takes into work only the last for some reason. - Yuri

https://docs.python.org/2/library/functions.html#open

 with open('yourfile.txt', 'rt') as f: for line in f: print(line) do_what_you_want(line) ... 

Keep in mind that strings are counted, including the newline character ( \r\n for Windows), to remove them, use line.rstrip('\n') or line.rstrip() - to remove all trailing whitespace.

  • Author Python has 3 script (by input () call for plain text). 'rt' is not needed in Python 3 (and in Python 2 it probably made sense only on Windows) - jfs
  • 'rt' and on the 2nd python is set by default, but I prefer to specify almost always explicitly. - andy.37
  • I do not quite understand how to insert this code into my example. - Yuri
  • mode == 'r' by default in Python. 't' (if you explicitly specify it). Python 2 sends to C stdio fopen ( such mode is not defined in C , but most platforms probably won't break). On Python 3, if 'b' not specified, then 't' implied. There is no need to 't' pass unless you try to exploit some platform-specific behavior. - jfs