![]() |
trying to write a dictionary in a csv file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: trying to write a dictionary in a csv file (/thread-36551.html) Pages:
1
2
|
trying to write a dictionary in a csv file - CompleteNewb - Mar-03-2022 I'm trying to write the dictionary straightlist in a csv file like this Rank , Hand 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']] ... import csv filename = 'rankedhands.csv' 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 csvfile: csvwriter = csv.DictWriter(csvfile, fieldnames = fields) csvwriter.writeheader() csvwriter.writerows(straightlist)but i get this error: can you help me please
RE: trying to write a dictionary in a csv file - deanhystad - Mar-03-2022 Your dictionary does not have any keys named "Rank" or "Hands". Your field names are 1, 2, 3,...9, not "Rank" and "Hands". DictWriter.writerows(straightlist) expects straightlist to be an iterable that returns dictionaries. This works: import csv filename = 'rankedhands.csv' 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']] } x = [{'Rank':key, "Hands":value} for key, value in straightlist.items()] # Make a list of dictionaries with open(filename, 'w') as csvfile: csvwriter = csv.DictWriter(csvfile, fieldnames = ["Rank", "Hands"]) csvwriter.writeheader() csvwriter.writerows(x)It produces output like this: This also works:with open(filename, 'w') as csvfile: csvwriter = csv.writer(csvfile) csvwriter.writerows(straightlist.items())It produces the same output sans header..
RE: trying to write a dictionary in a csv file - deanhystad - Mar-03-2022 This might be what you really want. filename = 'rankedhands.csv' 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 csvfile: csvwriter = csv.DictWriter(csvfile, fieldnames = range(1,10)) csvwriter.writeheader() csvwriter.writerow(straightlist) # Only 1 dictionary, so only 1 rowAnd this outputs:
RE: trying to write a dictionary in a csv file - CompleteNewb - Mar-03-2022 oh so you need a list of dictionaries... but it still gives me this error and i can't figure it out?
RE: trying to write a dictionary in a csv file - Larz60+ - Mar-03-2022 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:
RE: trying to write a dictionary in a csv file - bowlofred - Mar-03-2022 (Mar-03-2022, 11:23 PM)Larz60+ Wrote: I would save the dictionary to a json file.My thoughts exactly. Are you trying to produce a CSV file for external consumption, or are you writing it to save some sort of state for your program? If the latter, the CSV is much harder to work with. With JSON, you can just shove your entire object at it and read it back in later. RE: trying to write a dictionary in a csv file - CompleteNewb - Mar-03-2022 well, i don't even know what a JSON file is and i have other program that uses csv files so my priority is to learn how to manipulate csv files. JSON is something i want to learn to but it is very low on my list of priorities. Right now i tried to rewrite my program using your exemples and icame up with this filename = 'rankedhands.csv' fields = ['Rank','Hand'] 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']] } #x = [{'Rank':key, "Hands":value} for key, value in straightlist.items()] straightdictlist = [] for key, value in straightlist.items(): x = { 'Rank': '', 'Hand': '' } x['Rank'] = key x['Hand'] = value straightdictlist.append(x) print(straightdictlist) with open(filename, 'w') as csvfile: csvwriter = csv.DictWriter(csvfile, fieldnames = fields) csvwriter.writeheader() csvwriter.writerows(csvwriter.writerows(straightdictlist))and i get this error now the file was written ok but i dont understand the error?
RE: trying to write a dictionary in a csv file - deanhystad - Mar-04-2022 CSV files are extremely limited. If you want to save and restore information about the current state of your program. I strongly suggest using a JSON format file. RE: trying to write a dictionary in a csv file - CompleteNewb - Mar-04-2022 (Mar-04-2022, 12:17 AM)deanhystad Wrote: CSV files are extremely limited. If you want to save and restore information about the current state of your program. I strongly suggest using a JSON format file. i understand but i don't always choose the type of file that i have to deal with so i must learn to use csv RE: trying to write a dictionary in a csv file - bowlofred - Mar-04-2022 That's fine. But CSV only (easily) stores simple text fields. If you want to store a more complex data structure (like nested dicts or nested lists which is what your code shows), you're going to have to perform your own serialization. |