如何从文件中读取内容,然后将其保存到文件中?

时间:2019-05-27 06:40:35

标签: python python-3.x string hex

不太擅长编程。需要更改脚本代码。有必要读取文件的内容并将其拆分为单独的键,然后将其保存在文件中。预先感谢您的帮助!

'text.txt'

文件:

0200e7c810f4553fe1722522f8dcfc8e810757ef427efefef79bdf08ddf3700fd5 0216b3e68fed004b2fea2119cdbb8ab2393dfe8fc99398da18e40b6e949e9e1278 022bbf0fcde9bcba6e1038b78bd6906ed00be95d1a6f912a7352f5aca2d7bb6bbc 021060631ef4a610aebc3c9e24f5b0e33dcd0eb422b8223dbd75c1e6edfd21dd72 0218cbb66d6a417890aea6bf5f8a83a4d181a89c5aba8121e20def5b42c311514e 025d8ea956802ed00ebec42b480c0eb77c6ada6ed3fceb40e5fff9aed0fa31c6b4 02264a8c56551abeb68d6112863249857a4360c38528d02b9313988ba062e6efed

import binascii

with open('text.txt') as f:
    text = f.read()
compressed_key_hex = text.split('\n')

computed_uncompressed_key = []

p_hex = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F'
p = int(p_hex, 16)
x_hex = compressed_key_hex[2:66]
x = int(x_hex, 16)
prefix = compressed_key_hex[0:2]

y_square = (pow(x, 3, p)  + 7) % p
y_square_square_root = pow(y_square, (p+1)//4, p)
if prefix == "02":
    y = (-y_square_square_root) % p
else:
    y = y_square_square_root

computed_y_hex = hex(y)[2:66]
computed_uncompressed_key = "04" + x_hex + computed_y_hex

with open('result.txt', 'w') as f:
    f.write('\n'.join(computed_uncompressed_key))

我收到错误消息:

===================== RESTART: D:\detailALL\03\Bit06.py =====================
Traceback (most recent call last):
  File "D:\detailALL\03\Bit06.py", line 12, in <module>
    x = int(x_hex, 16)
TypeError: int() can't convert non-string with explicit base
>>>

1 个答案:

答案 0 :(得分:0)

您传递的是list而不是str。在以下代码中,x_hex是一个列表。

x_hex = compressed_key_hex[2:66]

因此,您需要将list转换为str,可以使用以下方法进行操作:

x_hex = ''.join(compressed_key_hex[2:66])

我想以下可能是您所需的解决方案:

import binascii

with open('text.txt') as f:
    text = f.read()
compressed_key_hex = text.split('\n')
print(compressed_key_hex)
computed_uncompressed_key_list = []
p_hex = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F'
p = int(p_hex, 16)
for val in compressed_key_hex:
    x_hex = val[2:66]
    x = int(x_hex, 16)
    prefix = val[0:2]

    y_square = (pow(x, 3, p) + 7) % p
    y_square_square_root = pow(y_square, (p+1)//4, p)
    if prefix == "02":
        y = (-y_square_square_root) % p
    else:
        y = y_square_square_root

    computed_y_hex = hex(y)[2:66]
    computed_y_hex = computed_y_hex.zfill(64)
    computed_uncompressed_key = "04" + x_hex + computed_y_hex
    computed_uncompressed_key_list.append(computed_uncompressed_key)
with open('result.txt', 'w') as f:
    f.write('\n'.join(computed_uncompressed_key_list))

text.txt文件:

0200e7c810f4553fe1722522f8dcfc8e810757ef427efefef79bdf08ddf3700fd5 
0216b3e68fed004b2fea2119cdbb8ab2393dfe8fc99398da18e40b6e949e9e1278 
022bbf0fcde9bcba6e1038b78bd6906ed00be95d1a6f912a7352f5aca2d7bb6bbc 
021060631ef4a610aebc3c9e24f5b0e33dcd0eb422b8223dbd75c1e6edfd21dd72 
0218cbb66d6a417890aea6bf5f8a83a4d181a89c5aba8121e20def5b42c311514e 
025d8ea956802ed00ebec42b480c0eb77c6ada6ed3fceb40e5fff9aed0fa31c6b4 
02264a8c56551abeb68d6112863249857a4360c38528d02b9313988ba062e6efed

result.txt文件:

0400e7c810f4553fe1722522f8dcfc8e810757ef427efefef79bdf08ddf3700fd5c9b034d2aa9ee1ef7b2346e8fc9c0245a8746a92bfdbb472fc98397477551ced
0416b3e68fed004b2fea2119cdbb8ab2393dfe8fc99398da18e40b6e949e9e12780126dfa95d2d9ab8fc055ce158f1d2ef51c2a012413b3f88a6365f375cf903f8
042bbf0fcde9bcba6e1038b78bd6906ed00be95d1a6f912a7352f5aca2d7bb6bbcf4a39790075ce43dc08fbf0ecc9cc732415e6b066c3b8b8d960b8548e8a612b7
041060631ef4a610aebc3c9e24f5b0e33dcd0eb422b8223dbd75c1e6edfd21dd723f873c976d071939edf8450124da64c3d9a1b35fb070761b01a5bace7d741588
0418cbb66d6a417890aea6bf5f8a83a4d181a89c5aba8121e20def5b42c311514efb4f8645c503e7a39954e977f7af8e802a5ec44ce3084cb6fb4e133a79733e77
045d8ea956802ed00ebec42b480c0eb77c6ada6ed3fceb40e5fff9aed0fa31c6b4e7c279c9d2c3e731803a4dde91a0d9409e49b1cbec3c7ac536a3783d9518d737
04264a8c56551abeb68d6112863249857a4360c38528d02b9313988ba062e6efeddbd8a97a8762f6a1add1ea6f549b61316fe675fc703d49f597a91ad620f7627a