我有一個打印功能。如何將其輸出為字符串?注意:我無法更改功能代碼。def f(): print('hello world')# do somethingassert x == 'hello world\n'我需要這個,因?yàn)榛鸹〝?shù)據(jù)框中有方法explain即打印約執(zhí)行計(jì)劃的信息。但我需要這個信息作為我的程序中的字符串。>>> df.explain()== Physical Plan ==Scan ExistingRDD[age#0,name#1]
1 回答

交互式愛情
TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個贊
def f():
print('hello world')
import io
from contextlib import redirect_stdout
with io.StringIO() as buf, redirect_stdout(buf):
f()
x = buf.getvalue()
assert x == 'hello world\n'
添加回答
舉報(bào)
0/150
提交
取消