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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:46:13+00:00 2026-05-13T12:46:13+00:00

I assumed sorting a CSV file on multiple text/numeric fields using Python would be

  • 0

I assumed sorting a CSV file on multiple text/numeric fields using Python would be a problem that was already solved. But I can’t find any example code anywhere, except for specific code focusing on sorting date fields.

How would one go about sorting a relatively large CSV file (tens of thousand lines) on multiple fields, in order?

Python code samples would be 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-05-13T12:46:14+00:00Added an answer on May 13, 2026 at 12:46 pm

    Here’s Alex’s answer, reworked to support column data types:

    import csv
    import operator
    
    def sort_csv(csv_filename, types, sort_key_columns):
        """sort (and rewrite) a csv file.
        types:  data types (conversion functions) for each column in the file
        sort_key_columns: column numbers of columns to sort by"""
        data = []
        with open(csv_filename, 'rb') as f:
            for row in csv.reader(f):
                data.append(convert(types, row))
        data.sort(key=operator.itemgetter(*sort_key_columns))
        with open(csv_filename, 'wb') as f:
            csv.writer(f).writerows(data)
    

    Edit:

    I did a stupid. I was playing with various things in IDLE and wrote a convert function a couple of days ago. I forgot I’d written it, and I haven’t closed IDLE in a good long while – so when I wrote the above, I thought convert was a built-in function. Sadly no.

    Here’s my implementation, though John Machin’s is nicer:

    def convert(types, values):
        return [t(v) for t, v in zip(types, values)]
    

    Usage:

    import datetime
    def date(s):
        return datetime.strptime(s, '%m/%d/%y')
    
    >>> convert((int, date, str), ('1', '2/15/09', 'z'))
    [1, datetime.datetime(2009, 2, 15, 0, 0), 'z']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 292k
  • Answers 292k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do this by modifying the registry. using Microsoft.Win32;… May 13, 2026 at 6:16 pm
  • Editorial Team
    Editorial Team added an answer You need to expand General.  May 13, 2026 at 6:16 pm
  • Editorial Team
    Editorial Team added an answer For the JFrame B, set the default close operation to… May 13, 2026 at 6:16 pm

Related Questions

How does the following code sort this array to be in numerical order? var
I am working on the I18n of our java application and we have decided
Hey all need a lil help sorting a loop for this table out, cant
I need to manually migrate modified stored procedures from a DEV SQL Server 2005
I am looking at the demo for sorting items here but I want the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.