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
Timus
  • 10,974
  • 5
  • 14
  • 28
zsyfrkn
  • 27
  • 3
  • 2
    Take a look at [csv.DictWriter](https://docs.python.org/3/library/csv.html#csv.DictWriter) (and the other classes in that module too) – GPhilo Mar 16 '22 at 09:14
  • 1
    So what is your question? Also, please, fix the indentation of your code, as well the quotes in e.g. `'11,'8'`. – buran Mar 16 '22 at 09:15
  • 1
    Does this answer your question? [Python Dictionary to CSV](https://stackoverflow.com/questions/8331469/python-dictionary-to-csv) – Ari Cooper-Davis Mar 16 '22 at 09: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 – tia.milani Mar 16 '22 at 09:17
  • Does this answer your question? [Write dictionary of lists to a CSV file](https://stackoverflow.com/questions/23613426/write-dictionary-of-lists-to-a-csv-file) – Timus Mar 16 '22 at 09:44

3 Answers3

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
JANO
  • 2,995
  • 2
  • 14
  • 29
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))
chitown88
  • 27,527
  • 4
  • 30
  • 59
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.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97