我一直在嘗試將這些資料寫入“scenario_out.txt”檔案。但是,當我運行代碼時,我沒有收到任何錯誤,但我的文本檔案確實是空的,所以沒有資料被放入檔案中。. 我正在查看我的筆記,但我不知道我哪里出錯了。
# Display results of scenario
def file_save_flow(save_to_file, # save to a file object if save_to_file = True
file_object, # name of file object to save to
scen_year,
age,
s_bal,
bal_pv,
inv_gains,
ann_savings,
ss_payment,
major_inc,
major_exp,
tax_rate,
tar_ret_spend,
net_ret_spend,
ret_pre_tax_spend,
taxes,
end_bal
):
""" file_save_flow() test output
>>> out_file = open("scenario_out.txt","w")
>>> file_save_flow(save_to_file = True,
... file_object = out_file,
... scen_year = 0,
... age = 60,
... s_bal = 100000,
... bal_pv = 100000,
... inv_gains = 1000,
... ann_savings = 1000,
... ss_payment = 1000,
... major_inc = 1000,
... major_exp = 1000,
... tax_rate = 0.15,
... tar_ret_spend = 50000,
... net_ret_spend = 40000,
... ret_pre_tax_spend = 60000,
... taxes = 10000,
... end_bal = 120000
... )
>>> out_file.close()
>>> in_file = open("scenario_out.txt","r")
>>> print(in_file.read())
Year :0
Age :60
Start Bal :100,000.00
Start Bal PV :100,000.00
Invest Gains :1,000.00
Ann Savings :1,000.00
SS Payment :1,000.00
Major Inc :1,000.00
Major Exp :1,000.00
Tax Rate :15.00%
Targ Ret Spend :50,000.00
Net Ret Spend :40,000.00
Ret Pre-tax Spend:60,000.00
Taxes :10,000.00
End Bal :120,000.00
<BLANKLINE>
>>> in_file.close()
"""
#=============================My Code================================
if save_to_file == True:
print(' Year :' , scen_year,
'\n Age :',age,
'\n Start Bal :', (format(s_bal, ',.2f')) ,
'\n Start Bal PV :', (format(bal_pv, ',.2f')),
'\n Invest Gains :', (format(inv_gains, ',.2f')),
'\n Ann Savings :', (format(ann_savings,',.2f')),
'\n SS Payment :', (format(ss_payment, ',.2f')),
'\n Major Inc :', (format(major_inc, ',.2f')),
'\n Major Exp :', (format(major_exp, ',.2f')),
'\n Tax Rate :', (format(tax_rate, '.2%')),
'\n Targ Ret Spend :', (format(tar_ret_spend, ',.2f')),
'\n Net Ret Spend :', (format(net_ret_spend, ',.2f')),
'\n Ret Pre-tax Spend :', (format(ret_pre_tax_spend, ',.2f')),
'\n Taxes :', (format(taxes, ',.2f')),
'\n End Bal :', (format(end_bal, ',.2f')))
#============================================================================
# test function
out_file = open("scenario_out.txt","w") # set up the file object to write to
file_save_flow(save_to_file = True,
file_object = out_file,
scen_year = 0,
age = 60,
s_bal = 100000,
bal_pv = 100000,
inv_gains = 1000,
ann_savings = 1000,
ss_payment = 1000,
major_inc = 1000,
major_exp = 1000,
tax_rate = 0.15,
tar_ret_spend = 50000,
net_ret_spend = 40000,
ret_pre_tax_spend = 60000,
taxes = 10000,
end_bal = 120000
)
out_file.close()
in_file = open("scenario_out.txt","r") # read back the contents saved to the file
print(in_file.read())
in_file.close()
我已經系結了一些 bool 變數用作 flag ,但仍然沒有運氣。
uj5u.com熱心網友回復:
目前,您只是將輸出列印到控制臺,而不是實際將其保存到檔案中。
你可能想要類似的東西
if save_to_file == True:
output = (
' Year : ' str(scen_year)
'\n Age : ' str(age)
'\n Start Bal : ' (format(s_bal, ',.2f'))
'\n Start Bal PV : ' (format(bal_pv, ',.2f'))
'\n Invest Gains : ' (format(inv_gains, ',.2f'))
'\n Ann Savings : ' (format(ann_savings,',.2f'))
'\n SS Payment : ' (format(ss_payment, ',.2f'))
'\n Major Inc : ' (format(major_inc, ',.2f'))
'\n Major Exp : ' (format(major_exp, ',.2f'))
'\n Tax Rate : ' (format(tax_rate, '.2%'))
'\n Targ Ret Spend : ' (format(tar_ret_spend, ',.2f'))
'\n Net Ret Spend : ' (format(net_ret_spend, ',.2f'))
'\n Ret Pre-tax Spend : ' (format(ret_pre_tax_spend, ',.2f'))
'\n Taxes : ' (format(taxes, ',.2f'))
'\n End Bal : ' (format(end_bal, ',.2f'))
)
file_object.write(output)
這會創建一大串輸出資料,然后將其寫入檔案物件。
uj5u.com熱心網友回復:
如果你去掉所有的絨毛,你的代碼看起來就像這樣:
def file_save_flow(save_to_file, file_object, data):
if save_to_file == True:
print('Data: ', data)
out_file = open("scenario_out.txt", "w")
file_save_flow(save_to_file=True, file_object=out_file, data='data')
out_file.close()
in_file = open("scenario_out.txt", "r")
print(in_file.read())
in_file.close()
從那以后,很明顯你的函式只做一件事:如果save_to_file是True,它列印一些東西,否則它什么都不列印。它對file_object.
因此,盡管您創建并打開一個檔案并將檔案句柄傳遞給您的函式,但您實際上并沒有在print()陳述句中使用它。
這會起作用:
def file_save_flow(save_to_file, file_object, data):
if save_to_file == True:
print('Data: ', data, file=file_object)
else:
print('Data: ', data)
或者,為了避免重復自己:
def file_save_flow(save_to_file, file_object, data):
print('Data: ', data, file=file_object if save_to_file else None)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/360983.html
上一篇:反序列化回傳空物件Java
