如何在此脚本中从file.txt导入python?

时间:2019-04-05 12:33:42

标签: python python-3.x cat

我有此代码,但我想从file.txt地址导入。 怎么修改?我试过“猫” “ python cashconverter.py output.txt” 但不起作用。谢谢

from cashaddress import convert

import sys
if len(sys.argv) != 2 :
    print("usage : bhcconvert <cashAddrss>")
else :
    addrss = sys.argv[1]
    addrss = addrss.replace('bitcoincash:' , '')
    if convert.is_valid('bitcoincash:' + addrss) :
        print( convert.to_legacy_address('bitcoincash:' + addrss))
    else :
        sys.stderr.write('Invalid address\n')

1 个答案:

答案 0 :(得分:0)

您可以通过两种方式进行操作(假设您是file.txt,每行有一个地址)

1。使用python

$ cat bhcconvert.py # assuming `bhcconvert.py` is the name of your script
from cashaddress import convert
import sys

def check():
  with open('file.txt') as f:
    for addrss in f:
      if addrss.strip():
        addrss = addrss.strip().replace('bitcoincash:' , '')
        if convert.is_valid('bitcoincash:' + addrss) :
          print( convert.to_legacy_address('bitcoincash:' + addrss))
        else :
          sys.stderr.write('Invalid address\n')

if __name__ == '__main__':
    check()

2。使用shell

while read -r address; do python bhcconvert.py "$address";done <file.txt