在Python中读取文件并将内容分配给变量

2024-05-21 06:36:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个文本文件中有一个ECC键值,我想将该值赋给一个变量以供进一步使用的一组行。虽然我可以从文件中读取键值,但我不知道如何将该值赋给变量。我不想把它当作一个数组。例如

变量=读取(public.txt)。

有什么建议吗?

python版本是3.4


Tags: 版本txt数组public建议键值文本文件ecc
2条回答
# Get the data from the file
with open('public.txt') as fp:
  v = fp.read()

# The data is base64 encoded. Let's decode it.
v = v.decode('base64')

#  The data is now a string in base-256. Let's convert it to a number
v = v.encode('hex')
v = int(v, 16)

# Now it is a number. I wonder what number it is:
print v
print hex(v)

或者,在Python3中:

#!/usr/bin/python3

import codecs

# Get the data from the file
with open('public.txt', 'rb') as fp:
  v = fp.read()

# The data is base64 encoded. Let's decode it.
v = codecs.decode(v,'base64')

#  The data is now a string in base-256. Let's convert it to a number
v = codecs.encode(v, 'hex')
v = int(v, 16)

# Now it is a number. I wonder what number it is:
print (v)
print (hex(v))

相关问题 更多 >