Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:40:48+00:00 2026-06-17T05:40:48+00:00

I have a large number of text files containg data arranged into a fixed

  • 0

I have a large number of text files containg data arranged into a fixed number of rows and columns, the columns being separated by spaces. (like a .csv but using spaces as the delimiter). I want to extract a given column from each of these files, and write it into 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') # all the text files have similar filename styles
        lines = f.readlines()   # read in the data
        no_lines = len(lines)   # get the number of lines

             for i in range (0,no_lines):
                 column = lines[i].strip().split(" ")

                 results_combined.write(column[5] + " " + '\r\n')

             f.close()

if __name__ == "__main__":
    combine_results()

This produces a text file containing the data I want from the separate files, but as a single column. (i.e. I’ve managed to ‘stack’ the columns on top of each other, rather than have them all side by side as separate columns). I feel I’ve missed something obvious.

In another attempt, I manage to write all the separate files to a single file, but without picking out 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()

What I basically want is to copy column 5 from each file (it is always the same column) and write them all to a single file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:40:49+00:00Added an answer on June 17, 2026 at 5:40 am

    If you don’t know the maximum number of rows in the files and if the files can fit into memory, then the following solution would work:

    import glob
    
    files = [open(f) for f in glob.glob("*.txt")]
    
    # Given file, Read the 6th column in each line
    def readcol5(f):
        return [line.split(' ')[5] for line in f]
    
    filecols = [ readcol5(f) for f in files ]
    maxrows = len(max(filecols, key=len))
    
    # Given array, make sure it has maxrows number of elements.
    def extendmin(arr):
        diff = maxrows - len(arr)
        arr.extend([''] * diff)
        return arr
    
    filecols = map(extendmin, filecols)
    
    lines = zip(*filecols)
    lines = map(lambda x: ','.join(x), lines)
    lines = '\n'.join(lines)
    
    fout = open('output.csv', 'wb')
    fout.write(lines)
    fout.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have split a large text file into a number of sets of smaller
I have a large number of text files (1000+) each containing an article from
I have a large text file that contains multiple columns of data. I'm trying
I have a large number of basic text, rtf, html, pdf and chm files
i have large numbers of text files and i am in problem that i
I have a large number of csv files that look like this below: xxxxxxxx
I have a large number of C# WCF services that are being called by
We have a large number of data in many categories with many properties, e.g.
I am trying to process a large number of text files. These text files
I have a text file that lists the names of a large number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.