0

I have dictionay list which looks like this:

{'match':['football','cricket','baseball'],'player':['2','11','8']}

I want to convert this dictionary into csv but without using pandas.

My code:

import csv
my_dictionary = {'match':['football','cricket','baseball'],'player'['2','11','8']}
with open('data.csv', 'w') as f:
    for key in my_dictionary.keys():
        f.write("%s, %s\n" % (key, my_dictionary[key]))

The output I would like to see:

match     player
football   2
cricket    11
baseball   8
5
  • 2
    Take a look at csv.DictWriter (and the other classes in that module too) Commented Mar 16, 2022 at 9:14
  • 1
    So what is your question? Also, please, fix the indentation of your code, as well the quotes in e.g. '11,'8'. Commented Mar 16, 2022 at 9:15
  • 1
    Does this answer your question? Python Dictionary to CSV Commented Mar 16, 2022 at 9:16
  • 1
    If you want to write line by line iterating over the keys doesn't make much sense. First, write the keys, then iterate over the two lists elements together zipping them. Please in the future avoid using "I want this and that" sentences, explain your problem, explain what you have tried but avoid sounding pretentious Commented Mar 16, 2022 at 9:17
  • Does this answer your question? Write dictionary of lists to a CSV file Commented Mar 16, 2022 at 9:44

3 Answers 3

0

So, if you want to use your technique, you can firstly write all keys into the first line as header and then iterate over the lists for the remaining rows like this:

import csv

my_dictionary = {'match':['football','cricket','baseball'],'player':['2','11','8']}
with open('data.csv', 'w') as f:
    
    headers = ', '.join(my_dictionary.keys()) + '\n'
    f.write(headers)
    
    for row in zip(*my_dictionary.values()):
        string_row = ', '.join(row) + '\n'
        f.write(string_row)

Output:

match, player
football, 2
cricket, 11
baseball, 8
Sign up to request clarification or add additional context in comments.

Comments

0

Use zip to combine those 2 lists ('match','player') into a list of tuples. Join each tuple with ,. Then simply write each of those to file.

import csv
my_dictionary = {'match':['football','cricket','baseball'],'player':['2','11','8']}
zipList = list(zip(my_dictionary['match'],my_dictionary['player']))
zipList = [','.join(x) for x in zipList]


with open('data.csv', 'w') as f:
    headers = ','.join(my_dictionary.keys()) + '\n'
    f.write(headers)
    for each in zipList:
        f.write("%s\n" % (each))

Comments

0

To avoid using Pandas, use the built in CSV library:

import csv
    
my_dictionary = {'match':['football','cricket','baseball'],'player':['2','11','8']}

with open('output.csv', 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(my_dictionary.keys())
    csv_output.writerows([*zip(*my_dictionary.values())])

This would produce output.csv containing:

match,player
football,2
cricket,11
baseball,8

Your dictionary keys give you the header, and if you use a standard *zip(*xxx) trick to transpose the entries you can write all the rows in one go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.