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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:18:05+00:00 2026-05-26T20:18:05+00:00

I have a CSV file with the following format: ID | STUFF | Custom

  • 0

I have a CSV file with the following format:

ID | STUFF |  Custom | Custom Value
1  | string1 | name1 | val1
1  | string1 | name2 | val2
1  | string1 | name3 | val3
2  | string2 | name1 | val4
2  | string2 | name3 | val5
3  | string3 | name2 | val6

etc…

The import part about the CSV is that the current Custom Column has various “Fields” in it that I need moved out to it’s own column and paired with it’s value in the next column. The Custom column contains somewhat unknown values. each ID, for example, may have a different subset of Custom “names”. I do, however, know the complete set of possible “Custom” names available.

Desired output: (NOTE: I realized I goofed on what I needed for the output, so now it’s corrected)

ID | STUFF    | name1       | name2      | name3

1  | SomeText | name1_Value | name2_Value| name3_Value
2  | SomeText | name1_Value | name2_Value| name3_Value

I am relatively new at Python and am having trouble seeing an elegant way of doing this without a serious amt of iterations/looping. I figured that using the CSV module and DictReader with tuples will probably end up being the right way of going about this, but I’m strugging with it at the moment. I have roughly 1200 rows in this file, and it only needs to work once, but I’d like to learn the best way to do things in python.

  • 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-26T20:18:06+00:00Added an answer on May 26, 2026 at 8:18 pm

    You could do something like this (assumes the rows in the csv are sorted by id):

    import csv, itertools, operator
    with open('data.csv', 'rb') as infile:
        results = []
        # uses the header row to get field names, each row will be a dict
        rows = csv.DictReader(infile)
        # keeps track of all the custom names we've seen
        all_custom_vals = set()
        for id_val, group in itertools.groupby(rows, operator.itemgetter('ID')):
            collapsed_row = {}
            for row in group:
                collapsed_row['ID'] = row['ID']
                collapsed_row['STUFF'] = row['STUFF']
                collapsed_row[row['Custom']] = row['Custom Value']
                all_custom_vals.add(row['Custom'])
            results.append(collapsed_row)
    

    itertools.groupby is really handy in situations like this.
    Then results will be a list of dictionaries, which you can write out as csv using something like this:

    import sys
    writer = csv.writer(sys.stdout)
    keys = sorted(all_custom_vals)
    writer.writerow(['ID', 'STUFF'] + keys)
    for row in results:
        items = [row['ID'], row['STUFF']]
        for key in keys:
            items.append(row.get(key, '<no value>'))
        writer.writerow(items)
    

    Replace <no value> with whatever the value should be when there was no row with that custom name.

    Edit: actually, the output I’ve given isn’t quite what you asked for (although I think it might be more useful). To get exactly what you asked for, you’d change the second part to be:

    import sys
    writer = csv.writer(sys.stdout)
    keys = sorted(all_custom_vals)
    for row in results:
        items = [row['ID'], row['STUFF']]
        for key in keys:
            items.append(key)
            items.append(row.get(key, '<no value>'))
        writer.writerow(items)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CSV file which has the following format: id,case1,case2,case3 Here is a
I have a csv file in a format resembling the following. There are no
I have a CSV file in the following format: id,code,date,address,complete_url Example data from the
I have a big csv files with the following format: CSV FILE 1 id,
I have csv files with the following format: CSV FILE a , b ,
I have a CSV file in following format which I wish to import in
If i have a CSV file that is in the following format fd!,sdf,dsfds,dsfd fd!,asdf,dsfds,dsfd
Assume you have a CSV file with the following simplified structure LINE1: ID,Description,Value LINE2:
I want the following functionality using php I have a csv file. Each file
I have a file that contains the following in a csv file; country,region,city,postalCode,metroCode,areaCode I

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.