2014-07-23 128 views
1

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

variable = read(public.txt)。

任何输入如何做到这一点?

的Python版本是3.4

+3

没有看到什么是在''public.txt''我们真的不能告诉你任何东西。显示该文件的示例,以及您想要的值。 – CoryKramer

+0

'变量=开放( 'public.txt')。读()' –

+0

MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEHERt50IOa5S03DPivsAMlg32uhJz yWV7XRGvP/8ca416BffPrflDoPeGbxwdpsZxbPwj2psvf/sehgukSrKoAw == @Cyber​​这是所有文件具有。我希望变量保持这个值。 – AshKsh

回答

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)) 
+0

添加'strip()'以满足新的要求:) – rslite

+0

我无法解码它,我得到一个属性错误为“AttributeError:'str'对象没有属性'decode'” – AshKsh

+0

@AshKsh - 你在使用Python3还是Python2? –

相关问题