简体   繁体   中英

How to insert a file with a list into a script?

I am trying to add a list file to a script. I need to make it so that to take the public key data from the "list.txt" file and save all the results to the "save.txt" file?

from bitcoinlib.keys import Address

master = Address ("0341b40ab5b2972161f2ff3d5487e0fb8260f2d98221cc2eb4fa3f28b6ad10d81e", encoding = 'bech32', script_type = 'p2wpkh')
print (master.address)

At the moment I am getting one value

bc1q7wdz5dcs553f2y6qgf38xdgqs2kqgkhn5ydn9l

How to fix that in place of this value: 0341b40ab5b2972161f2ff3d5487e0fb8260f2d98221cc2eb4fa3f28b6ad10d81e

There was a list of this file "list.txt"

02485a4e62913be3db116d1ab15f84110599ea8905cd7dbae7be6fa02033fdb54e
0315da5f8f47787f6e8294bd369a4dd81aea97429630ecae831a9f6362a6917106
023741e71ebddc5eca046c9b23ac7c5230160fe1335e655c9bbe0b8a20c8d89802
037782a3fcc6c0ca092658a513c9f051cc95d540d215f0c965176c664d49d3e732
029c6c7748107fc9584a838df6a2c8224ae2339e2a95b15b4cd8bcc67c2d149cd5

To get all the value and save to the file "save.txt"

bc1q6jxrahx3rw6lt2nlv5fpsdtllyzaa03m4d98xv
bc1qct3fu8543tryapkq4kpgw5ph8cj74zhtrdp5sx
bc1q5a3h25vu4kn90sc70rkm65narezzw97khu4dhu
bc1qutzkrtc7tqqjgrzns3s3h92f8wfxvfhp99ppnn
bc1ql2slqxzp7c9hdxhlp0ehlzdg2qa94xh5lk2anw

Please help me with fixing the code!

As far as i'm concerned you want to use each line of file separately. First read all the lines to list:

with open('list.txt', 'r') as f:
   lists = [i.replace('\n', '') for i in f.readlines()]

Then for each line, create Address instance, and save it to another list:

addresses = []
for l in lists:
    master = Address(l, encoding = 'bech32', script_type = 'p2wpkh')
    addresses.append(master.address)

The last part is to save all to file save.txt

with open('save.txt', 'w+') as f:
   for a in addresses:
      f.write(a + '\n')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM