2014-07-23 123 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? –

相關問題