繁体   English   中英

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

[英]Reading from a file and assigning content to a variable in Python

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

变量 = 读取(public.txt)。

有关如何执行此操作的任何输入?

python版本是3.4

# 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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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