0

This is my dictionary structure

dic = {'tt3832096': ['remake', 'horror-movie-remake', 'flesh-eating-virus', 'gore'],
       'tt6217804': ['chainsaw', 'sequel', 'second-part', 'mable-simmons-character']}

I want to save it in csv file like this:

movie id      keyword
tt3832096     ['remake', 'horror-movie-remake', 'flesh-eating-virus', 'gore']
tt6217804     ['chainsaw', 'sequel', 'second-part', 'mable-simmons-character']

I have tried this:

with open('test.csv', 'w') as f:
    for key in dic.keys():
        f.write("%s:%s\n"%(key,dic[key]))

But it places each term of value in a cell. Is there a better way?

jpp
  • 159,742
  • 34
  • 281
  • 339
  • 1
    what have you tried so far? – Dschoni Nov 05 '18 at 16:00
  • There are plenty of sources for this. Check our [this](https://stackoverflow.com/questions/10373247/how-do-i-write-a-python-dictionary-to-a-csv-file), or [this](https://stackoverflow.com/questions/8331469/python-dictionary-to-csv). – luke Nov 05 '18 at 16:06
  • A proper csv structure would be to have one line per movie_id/keyword pair. – bruno desthuilliers Nov 05 '18 at 16:06

3 Answers3

0

You can use csv.writer with tabs as the delimiter:

import csv
d = {'tt3832096': ['remake', 'horror-movie-remake', 'flesh-eating-virus', 'gore'], 'tt6217804': ['chainsaw', 'sequel', 'second-part', 'mable-simmons-character']}
with open('output.csv', 'w') as f:
    writer = csv.writer(f, delimiter='\t')
    writer.writerow(['movie id', 'keyword'])
    for k, v in d.items():
        writer.writerow([k, v])
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

If you can use a 3rd party library, this is trivial with Pandas:

import pandas as pd

# construct dataframe from dictionary d
df = pd.DataFrame(list(d.items()), columns=['movie id', 'keyword'])

# export to csv
df.to_csv('file.csv', index=False)

Really, though, this is not recommended if you plan to read the file back into Python. In this situation, you can use Pickle (version dependent) to store and pick up your dictionary with list values:

import pickle

with open('d_file.pkl', 'wb') as fout:
    pickle.dump(d, fout, protocol=pickle.HIGHEST_PROTOCOL)

with open('d_file.pkl', 'rb') as fin:
    d = pickle.load(fin)
jpp
  • 159,742
  • 34
  • 281
  • 339
0

The lines you showed, like this:

 tt3832096     ['remake', 'horror-movie-remake', 'flesh-eating-virus', 'gore']

are not part of a CSV file or, at least, not a CSV file with the structure you're thinking of. As a CSV record, this is a line with four values which look like this (or something like this, depending on how quote characters are treated when decoding).

 tt3832096     ['remake'
 'horror-movie-remake'
 'flesh-eating-virus'
 'gore']

If you really want to save this as a CSV file, your best bet would be something that looks like this:

 'tt3832096', 'remake', 'horror-movie-remake', 'flesh-eating-virus', 'gore'

which should be similar to what you're producing now.

If you want to store it in a format that is more like the dictionary it came from, you have a few options:

  1. Grow your own format (like the one you described) and read and write it as text (not using a CSV library).
  2. Use the built-in Python serialization library pickle which will preserve the structure but produces files that are not human readable.
  3. Use JSON serialization using one of the json libraries, which produces human readable, standard json files that can preserve dictionary structures.
Larry Lustig
  • 49,320
  • 14
  • 110
  • 160