[英]Passing a JSON document in Python
我正在嘗試傳遞要編碼為base64的策略文檔。
策略文檔位於~/policy_document
>>> policy = base64.b64encode(policy_document)
我需要做什么才能將policy_document
傳遞給base64? 謝謝。
# First open the file
# Then read the entire contents into memory
>>> policy_document = open("/absolute/path/to/policy_document", "r").read()
# Then base64 encode the contents.
>>> policy = base64.b64encode(policy_document)
# If you are using Python 2.7 you can use the with statement
# to ensure files are cleaned up
# (See @Niklas' comment)
>>> with open("/absolute/path/to/policy_document", "r") as fp:
... policy_document = fp.read()
... policy = base64.b64encode(policy_document)
# fp will be properly closed
或者,如果您需要它來自當前用戶的主文件夾,則可以添加對os.path.expanduser("~/policy_document")
的調用
這為我工作:
policy = base64.b64encode(json.JSONEncoder().encode({dict})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.