Python Forum
trying to write a dictionary in a csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to write a dictionary in a csv file
#5
I would save the dictionary to a json file.

The example below includes a test routine and dictionary display routine which you don't need for your app.

import csv
import os
import json


filename = 'rankedhands.json'

def save_rankedhands():
    # Make sure cwd same as script
    os.chdir(os.path.abspath(os.path.dirname(__file__)))

    fields = ['Rank','Hands']

    straightlist = {
        1: [['10', 'X'], ['11', 'X'], ['12', 'X'], ['13', 'X'], ['14', 'X']],
        2: [['9', 'X'], ['10', 'X'], ['11', 'X'], ['12', 'X'], ['13', 'X']],
        3: [['8', 'X'], ['9', 'X'], ['10', 'X'], ['11', 'X'], ['12', 'X']],
        4: [['7', 'X'], ['8', 'X'], ['9', 'X'], ['10', 'X'], ['11', 'X']],
        5: [['6', 'X'], ['7', 'X'], ['8', 'X'], ['9', 'X'], ['10', 'X']],
        6: [['5', 'X'], ['6', 'X'], ['7', 'X'], ['8', 'X'], ['9', 'X']],
        7: [['4', 'X'], ['5', 'X'], ['6', 'X'], ['7', 'X'], ['8', 'X']],
        8: [['3', 'X'], ['4', 'X'], ['5', 'X'], ['6', 'X'], ['7', 'X']],
        9: [['2', 'X'], ['3', 'X'], ['4', 'X'], ['5', 'X'], ['6', 'X']]
    }

    with open(filename, 'w') as jfile:
        json.dump(straightlist, jfile)

def display_dict(dictname, level=0):
    indent = " " * (4 * level)
    for key, value in dictname.items():
        if isinstance(value, dict):
            print(f'\n{indent}{key}')
            level += 1
            display_dict(value, level)
        else:
            print(f'{indent}{key}: {value}')
        if level > 0:
            level -= 1

def test_save_rankedhands():
    # Save dictionary to json file
    save_rankedhands()

    # read json file into newdict and display
    with open(filename) as jfile:
        newdict = json.load(jfile)

    display_dict(newdict)


if __name__ == '__main__':
    test_save_rankedhands()
Test results:
Output:
1: [['10', 'X'], ['11', 'X'], ['12', 'X'], ['13', 'X'], ['14', 'X']] 2: [['9', 'X'], ['10', 'X'], ['11', 'X'], ['12', 'X'], ['13', 'X']] 3: [['8', 'X'], ['9', 'X'], ['10', 'X'], ['11', 'X'], ['12', 'X']] 4: [['7', 'X'], ['8', 'X'], ['9', 'X'], ['10', 'X'], ['11', 'X']] 5: [['6', 'X'], ['7', 'X'], ['8', 'X'], ['9', 'X'], ['10', 'X']] 6: [['5', 'X'], ['6', 'X'], ['7', 'X'], ['8', 'X'], ['9', 'X']] 7: [['4', 'X'], ['5', 'X'], ['6', 'X'], ['7', 'X'], ['8', 'X']] 8: [['3', 'X'], ['4', 'X'], ['5', 'X'], ['6', 'X'], ['7', 'X']] 9: [['2', 'X'], ['3', 'X'], ['4', 'X'], ['5', 'X'], ['6', 'X']]
Reply


Messages In This Thread
RE: trying to write a dictionary in a csv file - by Larz60+ - Mar-03-2022, 11:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace values in Yaml file with value in dictionary PelleH 0 169 Jun-12-2024, 02:40 PM
Last Post: PelleH
  What does .flush do? How can I change this to write to the file? Pedroski55 3 451 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 607 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,929 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,879 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 8,401 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,257 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,890 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 11,146 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  How to write in text file - indented block Joni_Engr 4 6,710 Jul-18-2022, 09:09 AM
Last Post: Hathemand

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020