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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:15:08+00:00 2026-05-22T21:15:08+00:00

I have several files, each of which has data like this (filename:data inside separated

  • 0

I have several files, each of which has data like this (filename:data inside separated by newline):

  1. Mike: Plane\nCar
  2. Paula: Plane\nTrain\nBoat\nCar
  3. Bill: Boat\nTrain
  4. Scott: Car

How can I create a csv file using python that groups all the different vehicles and then puts a X on the applicable person, like:

output

  • 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-22T21:15:09+00:00Added an answer on May 22, 2026 at 9:15 pm

    Assuming those line numbers aren’t in there (easy enough to fix if they are), and with an input file like following:

    Mike: Plane
    Car
    Paula: Plane
    Train
    Boat
    Car
    Bill: Boat
    Train
    Scott: Car
    

    Solution can be found here : https://gist.github.com/999481

    import sys
    from collections import defaultdict
    import csv
    
    # see http://stackoverflow.com/questions/6180609/group-and-check-mark-using-python
    def main():
        # files = ["group.txt"]
        files = sys.argv[1:]
        if len(files) < 1:
            print "usage: ./python_checkmark.py file1 [file2 ... filen]"
    
        name_map = defaultdict(set)
    
        for f in files:
            file_handle = open(f, "r")
            process_file(file_handle, name_map)
            file_handle.close()
    
        print_csv(sys.stdout, name_map) 
    
    def process_file(input_file, name_map):
        cur_name = ""
        for line in input_file:
            if ":" in line:
                cur_name, item = [x.strip() for x in line.split(":")]
            else:
                item = line.strip()
            name_map[cur_name].add(item)
    
    
    def print_csv(output_file, name_map):
        names = name_map.keys()
        items = set([])
        for item_set in name_map.values():
            items = items.union(item_set)
    
        writer = csv.writer(output_file, quoting=csv.QUOTE_MINIMAL)
        writer.writerow( [""] + names )
        for item in sorted(items):
            row_contents = map(lambda name:"X" if item in name_map[name] else "", names)
            row = [item] + row_contents
            writer.writerow( row )
    
    
    if __name__ == '__main__':
        main()
    

    Output:

    ,Mike,Bill,Scott,Paula 
    Boat,,X,,X 
    Car,X,,X,X 
    Plane,X,,,X 
    Train,,X,,X 
    

    Only thing this script doesn’t do is keep the columns in order that the names are in. Could keep a separate list maintaining the order, since maps/dicts are inherently unordered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have many projects with several files inside each. Files can be checked in
I have several billion rows of data in CSV files. Each row can have
I have a spring web application which has several modules. Each module has its
I have several hundred files in a non-flat directory structure. My Makefile lists each
we have several tens of macro enabled excel files, each of those contains few
I have a project with several CSS files, each with many different settings. Every
I have an iOS application with several controllers, each with their own xib files.
I have a CSV file with several entries, and each entry has 2 unix
I have a text file with several different sections. Each section has a header
I have several files which is causing a compile error, can the following be

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.