2017-08-11 49 views
1

我正在使用python使一些SQL查詢我必須爲我的JDBC連接更模塊化,並希望用打印語句執行我的字符串文字部分。python打印執行字符串文字作爲代碼的一部分

fh = open("data.txt","w") 
print("42 is the answer, but what is the question?", file=fh) 
fh.close() 

我知道我可以用它來寫文本,但我想我打印的輸出保存在一個局部變量在其他語句再來拜訪。

這可能嗎?

+0

謝謝編輯Wondercricket。 – Flufylobster

回答

1
output = "42 is the answer, but what is the question?" 
with open("data.txt","w") as fh: 
    fh.write(output) 
+0

然後只讀它是一個字符串文字? – Flufylobster

+0

@Flufylobster我不明白你的意思。明確你的問題。 – smac89