I have a large number of text files containing data arranged in a fixed number of rows and columns, with the columns separated by spaces. (like .csv, but using spaces as a delimiter). I want to extract a given column from each of these files and write it to a new text file.
So far I have tried:
results_combined = open('ResultsCombined.txt', 'wb') def combine_results(): for num in range(2,10): f = open("result_0."+str(num)+"_.txt", 'rb')
This creates a text file containing the data I want from separate files, but as a single column. (i.e., I managed to "stack" the columns on top of each other, and not combine them with each other as separate columns). I feel like I missed something obvious.
In another attempt, I manage to write all the individual files to a single file, but without highlighting the columns that I want.
import glob files = [open(f) for f in glob.glob("result_*.txt")] fout = open ("ResultsCombined.txt", 'wb') for row in range(0,488): for f in files: fout.write( f.readline().strip() ) fout.write(' ') fout.write('\n') fout.close()
Basically I want to copy column 5 from each file (it is always the same column) and write them to a single file.
source share