我正在将凭证文件读入字符串,然后在该字符串上运行exec()
以使各种凭据可用作变量。凭证文件包含如下文本:
customer_number = "9999999999"
PIN = "9999999999"
passcode = "9999999999"
account_code = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
这适用于交互式Python(变量可用)但在脚本中失败。为什么这样,我怎么才能让它发挥作用?
with open(filepath_credentials, "r") as file_credentials:
credentials = file_credentials.read()
exec(credentials)
print(customer_number)
答案 0 :(得分:1)
您需要将.moveable {
width: 100px;
height: 100px;
position: absolute;
}
#div1 {
background-color: green;
top: 10px;
left: 10px;
}
#div2 {
background-color: red;
top: 40px;
left: 40px;
}
#div3 {
background-color: yellow;
top: 20px;
left: 70px;
}
(可能还有<div>
<div id="div1" class="moveable">Click</div>
<div id="div2" class="moveable">Click</div>
<div id="div3" class="moveable">Click</div>
</div>
)集合传递给globals()
:
locals()
这允许exec()
修改脚本的全局变量。 Python交互式解释器的工作方式略有不同。