【问题标题】:Python just executed 1 linePython 刚刚执行了 1 行
【发布时间】:2018-11-06 14:50:59
【问题描述】:

嗨,我是 python 代码的新手。我试图制作一个程序来从十六进制生成私钥。 我将十六进制列表制作为 .txt 之类的 800000000000000000000000000000000000000000000000

并导入代码

当我尝试执行文件时,只执行第一行 而在txt文件中有5行

有没有办法执行所有列表? 我的代码错误在哪里?

output_file = open("output.txt", "w")

# Step 2: let's add 80 in front of it
with open("generate.txt", "r") as extended_key:
    data = extended_key.readline().replace("\n", "")


# Step 3: first SHA-256
first_sha256 = hashlib.sha256(binascii.unhexlify(data)).hexdigest()
# Step 4: second SHA-256
second_sha256 = hashlib.sha256(binascii.unhexlify(first_sha256)).hexdigest()
# Step 5-6: add checksum to end of extended key
final_key = data+second_sha256[:8]
# Step 7: finally the Wallet Import Format is the base 58 encode of final_key
WIF = base58.b58encode(binascii.unhexlify(final_key))

output_file.write (WIF)

【问题讨论】:

  • .readline() 只读取文件的第一行。请改用.read()

标签: python loops sha256


【解决方案1】:

你应该使用 readlines() 来获得一个包含所有行的列表,然后遍历这个列表。

【讨论】:

  • 请考虑对答案进行评分。
  • 建议数据 = extended_key.readlines()。这将为您提供一个列表,其中包含文件中不带 \n 的所有行。然后你可以遍历列表并做所有的摘要。您可以选择将其放入新列表或相同列表中,然后使用 output_file.writelines(data) 写入完整列表
【解决方案2】:

用这个替换上面的部分。 fileOb.read() 读取整个内容,而 fileOb.readline() 只读取一行。

# Step 2: let's add 80 in front of it
with open("generate.txt", "r") as extended_key:
    data = extended_key.read().replace("\n", " ") #to read all the lines and replace the newline characters by spaces

【讨论】:

  • 我试过了,但它会产生没有空格的长单词
  • 它应该可以工作。使用调试器检查变量显示意外行为的位置。
  • heres the respons 800000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000005
  • 所以你希望换行符被空格“”替换?
猜你喜欢
  • 1970-01-01
  • 2011-02-24
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-11
  • 2018-03-09
相关资源
最近更新 更多