簡體   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