-4

Here;s the calc_property_statistics function which returns maximum, minimum, and average values. I need to write them to a file.

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)
Rajiv Sharma
  • 6,746
  • 1
  • 52
  • 54
  • 2
    What is your question? Have you had a look at https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files? Who is going to read the file later (a script or a human)? Why did you tag the question `fwrite` which has nothing to do with Python? – Tim Pietzcker Jul 26 '16 at 06:23
  • That is a nice function, however it has absolutely nothing to do with what you appear to be asking, if you want to know how to write to a file then [look at some resources on that](https://docs.python.org/3/tutorial/inputoutput.html) before asking here. – Tadhg McDonald-Jensen Jul 26 '16 at 06:58
  • Possible duplicate of [Python Print String To Text File](http://stackoverflow.com/questions/5214578/python-print-string-to-text-file) – tripleee Jul 26 '16 at 07:02

3 Answers3

1

this is the example of function to write return values from other function to file:

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()
Rajiv Sharma
  • 6,746
  • 1
  • 52
  • 54
  • This works for strings. In the OP's example, it seems you're dealing with multiple values. Depending on who uses the data later, it might be necessary to take that into account. Also, you don't need to close the file when you're already using a `with` statement to handle precisely that. – Tim Pietzcker Jul 26 '16 at 06:36
  • @Tim Pietzcker thanks for the tip regarding ( with statement ). – Rajiv Sharma Jul 26 '16 at 06:40
0

at first it depends from format of file you need to use.

this is example how to write into simple text file with values separated by newline.

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()

or it can be done with one "write"

...
out_file.write('{}\n{}\n{}\n'.format(x[0], x[1], x[2]))
...
Mike
  • 1
  • 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()
Rajiv Sharma
  • 6,746
  • 1
  • 52
  • 54