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 8723375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:36:44+00:00 2026-06-13T07:36:44+00:00

I have a file that has columns that look like this: Column1,Column2,Column3,Column4,Column5,Column6 1,2,3,4,5,6 1,2,3,4,5,6

  • 0

I have a file that has columns that look like this:

Column1,Column2,Column3,Column4,Column5,Column6
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
Column1,Column3,Column2,Column6,Column5,Column4
1,3,2,6,5,4
1,3,2,6,5,4
1,3,2,6,5,4
Column2,Column3,Column4,Column5,Column6,Column1
2,3,4,5,6,1
2,3,4,5,6,1
2,3,4,5,6,1

The columns randomly re-order in the middle of the file, and the only way to know the order is to look at the last set of headers right before the data (Column1,Column2, etc.) (I’ve also simplified the data so that it’s easier to picture. In real life, there is no way to tell data apart as they are all large integer values that could really go into any column)

Obviously this isn’t very SQL Server friendly when it comes to using BULK INSERT, so I need to find a way to arrange all of the columns in a consistent order that matches my table’s column order in my SQL database. What’s the best way to do this? I’ve heard Python is the language to use, but I have never worked with it. Any suggestions/sample scripts in any language are appreciated.

  • 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-13T07:36:45+00:00Added an answer on June 13, 2026 at 7:36 am

    A solution in python:

    I would read line-by-line and look for headers. When I find a header, I use it to figure out the order (somehow). Then I pass that order to itemgetter which will do the magic of reordering elements:

    from operator import itemgetter
    def header_parse(line,order_dict):
        header_info = line.split(',')
        indices = [None] * len(header_info)
        for i,col_name in enumerate(header_info):
            indices[order_dict[col_name]] = i
        return indices
    
    def fix(fname,foutname):
        with open(fname) as f,open(foutname,'w') as fout:
            #Assume first line is a "header" and gives the order to use for the
            #rest of the file
            line = f.readline()
            order_dict = dict((name,i) for i,name in enumerate(line.strip().split(',')))
            reorder_magic = itemgetter(*header_parse(line.strip(),order_dict))
            for line in f:
                if line.startswith('Column'):  #somehow determine if this is a "header"
                    reorder_magic = itemgetter(*header_parse(line.strip(),order_dict))
                else:
                    fout.write(','.join(reorder_magic(line.strip().split(','))) + '\n')
    
    if __name__ == '__main__':
        import sys
        fix(sys.argv[1],sys.argv[2])
    

    Now you can call it as:

    python fixscript.py badfile goodfile
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files, that look like this: File 1 (2 columns): ID1 123
I have a test file that has many lines, each line looks something like:
I have a html file that has invoice details I would like to know
I have an xml file that has an address node that looks like <address>
I have a file that has 2 columns as given below.... 101 6 102
I have a file that has two columns of floating point values. I also
I have a csv file that has 2 columns separated by a comma -
I have a file that looks like this: 1st ­ ­ ­ ­­­ 2nd
I have a file that has the following format: 12345 TAB_HERE Name : The
I have an XML file that has a number of nodes, each of which

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.