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

Ash*_*Ksh 4 python

我在文本文件(一组行)中有一个 ECC 密钥值。我想将该值分配给变量以供进一步使用。虽然我可以从文件中读取键值,但我不知道如何将该值分配给变量。我不希望它作为一个数组。例如:

variable = read(public.txt)
Run Code Online (Sandbox Code Playgroud)

我使用的是Python 3.4

Rob*_*obᵩ 7

# 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)
Run Code Online (Sandbox Code Playgroud)

或者,在 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))
Run Code Online (Sandbox Code Playgroud)