简体   繁体   中英

Saving an output as .txt file from for-loop for a function

I am a beginner in python and trying to save these the output for loop calculation to a .txt file.

I have an input data with 40 columns and 300 rows. I am putting sample data:

4.3 3.4 2.3
8.5 1.0 0.0
2.4 4.2 4.5
3.9 2.1 0.5

import numpy as np

q = np.loadtxt('test.txt')

def qtot(i):
    total = 0
    for j in range (3):
        total += q[i][j]
    return total

I would like to save qtot into an output file. Any help would be truly appreciated.

You have read your 300 lines file into memory and it is a numpy array. Let's do your simple computation inside numpy and use it to save everything into a text file.

import numpy as np
np.savetxt("output.txt", np.sum( np.loadtxt('test.txt'),axis=1 )[0:5] )

How does it work.....

  1. np.load - loads the file and it is 2d array
  2. np.sum - sums elements in the array, but axis=1 tells numpy that sum should go in raws so it returns an array with the same number of elements as lines in your file
  3. [0:5] - cuts down this array to just the first five elements. You can use slices or tuples to cut required lines from the array
  4. np.savetxt - saves it into a file

Note that numpy does everything in low-level c-like code. So summing by raw for all 300 lines will be faster than a loop in python.

--ADD--

If you want to add your results to the file, you should open it in the append mode:

with open(output_file, "a") as fd:
   np.savetxt(fd, np.sum( np.loadtxt('test.txt'),axis=1 )[0:5] )

but if you want to process multiple files inside the same run, it is better to accumulate results in the memory and save once

result = []
for fname in [list of input files]:
   result.append( np.sum( np.loadtxt(fname),axis=1 )[0:5] )
np.saavetxt("output.txt", np.array(result))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM