此处; calc_property_statistics
函数返回最大值,最小值和平均值。我需要将它们写入文件。
def calc_property_statistics(prop, realisation=0):
values = prop.get_values(realisation)
maximum = np.max(values)
minimum = np.min(values)
average = np.average(values)
print("maximum: {}, minimum: {}, average: {} for property {}".format(
maximum,
minimum,
average,
prop))
return (maximum, minimum, average)
答案 0 :(得分:1)
这是将其他函数的返回值写入文件的函数示例:
def my_func():
"""
this function return some value
:return:
"""
return 'This is some value'
def write_file(data):
"""
this function write data to file
:param data:
:return:
"""
file_name = r'D:\log.txt'
with open(file_name, 'wb') as x_file:
x_file.write(data)
def run():
data = my_func()
write_file(data)
run()
答案 1 :(得分:0)
起初它取决于您需要使用的文件格式。
这是如何写入具有由换行符分隔的值的简单文本文件的示例。
x = calc_property_statistics(prop, realisation)
out_file = open('results.txt', 'wb')
out_file.write(x[0]) # maximum
out_file.write('\n')
out_file.write(x[1]) # minimum
out_file.write('\n')
out_file.write(x[2]) # average
out_file.write('\n')
out_file.close()
或者可以用一个“写”来完成
...
out_file.write('{}\n{}\n{}\n'.format(x[0], x[1], x[2]))
...
答案 2 :(得分:-1)
f=open('abc.txt','w')
def calc_property_statistics(prop, realisation=0):
values = prop.get_values(realisation)
maximum = np.max(values)
minimum = np.min(values)
average = np.average(values)
return (maximum, minimum, average)
#--------------------------------------------------------------
# Main script body
x = calc_property_statistics(project.grid_models['Heterogeneity'].properties['Poro'])
f.write("%s Maximum\n %s Minimum \n %s Average \n" %x)
f.close()