python打印以作为代码的一部分执行字符串文字
我正在使用python使JDBC连接的一些SQL查询更加模块化,并希望使用print语句执行部分字符串文本python打印以作为代码的一部分执行字符串文字,python,Python,我正在使用python使JDBC连接的一些SQL查询更加模块化,并希望使用print语句执行部分字符串文本 fh = open("data.txt","w") print("42 is the answer, but what is the question?", file=fh) fh.close() 我知道我可以用它来写文本,但我想把打印输出保存在一个局部变量中,以便以后在其他语句中调用 fh = open("data.txt","w") print("42 is the answer,
fh = open("data.txt","w")
print("42 is the answer, but what is the question?", file=fh)
fh.close()
我知道我可以用它来写文本,但我想把打印输出保存在一个局部变量中,以便以后在其他语句中调用
fh = open("data.txt","w")
print("42 is the answer, but what is the question?", file=fh)
fh.close()
这可能吗?谢谢您编辑Wondercricket.@Flufylobster我不明白您的意思。把你的问题说清楚。
output = "42 is the answer, but what is the question?"
with open("data.txt","w") as fh:
fh.write(output)