NaturallyGreezy v2
NaturallyGreezy v2

Reputation: 65

Unsure how to write dictionary/list to file Python3

I'm trying to create a simple database that uses five fields on python3. I want to create two different procedures (for the time being) with one which is where the data is all stored, and another where I will copy this data into a new file. I feel the best way to do this is to use a dictionary and/or list. My code so far is as follows:

def createFile(allColours):
    colours = open("colours","w")
    colours.write(allColours)

def data():
    allColours = []               
    allColours.append = ({"ID": "1", "Shade": "Black", "Red": "0", "Green": "0", "Blue":"0"})
    allColours.append = ({"ID": "2", "Shade": "White", "Red": "255", "Green": "255", "Blue":"255"})
    allColours.append = ({"ID": "3", "Shade": "Red", "Red": "255", "Green": "0", "Blue":"0"})
    allColours.append = ({"ID": "4", "Shade": "Green", "Red": "0", "Green": "255", "Blue":"0"})
    allColours.append = ({"ID": "5", "Shade": "Blue", "Red": "0", "Green": "0", "Blue":"255"})
    createFile(allColours)

When I try this, I get the error code AttributeError: 'list' object attribute 'append' is read-only and I'm not sure what I'm doing wrong. I'm also uncertain as to whether I'm doing the right thing in creating a new file with the colours.

Upvotes: 1

Views: 81

Answers (2)

Harvey
Harvey

Reputation: 5821

You should pick a formatting for saving your data. Here's your code modified to use json:

#!/usr/bin/env python3

import json


def createFile(allColours):
    with open("colours.json", "w") as f:
        f.write(json.dumps(allColours, indent=4))


def data():
    allColours = []
    allColours.append({"ID": "1", "Shade": "Black", "Red": "0", "Green": "0", "Blue":"0"})
    allColours.append({"ID": "2", "Shade": "White", "Red": "255", "Green": "255", "Blue":"255"})
    allColours.append({"ID": "3", "Shade": "Red", "Red": "255", "Green": "0", "Blue":"0"})
    allColours.append({"ID": "4", "Shade": "Green", "Red": "0", "Green": "255", "Blue":"0"})
    allColours.append({"ID": "5", "Shade": "Blue", "Red": "0", "Green": "0", "Blue":"255"})
    createFile(allColours)


if __name__ == '__main__':
    data()

Here's another way to write your data function that it a bit easier to maintain:

from collections import OrderedDict


def data2():
    keys = ("ID", "Shade", "Red", "Green", "Blue")
    values = (
        (1, "Black", 0, 0, 0),
        (2, "White", 255, 255, 255),
        (3, "Red", 255, 0, 0),
        (4, "Green", 0, 255, 0),
        (5, "Blue", 0, 0, 255),
    )
    # An OrderedDict will preserve the key order in the json output
    allColours = [OrderedDict(zip(keys, colour)) for colour in values]
    createFile(allColours)

Upvotes: 0

Tezirg
Tezirg

Reputation: 1649

Python list are classes with attributes. Here the append attribute is a method, and you are trying to re-assign its value. You should just use the function instead.

In python 3 you can append to a list like so :

my_list = []
#Using member function append
my_list.append({"ID": 0})
# using [] operator
my_list[len(my_list)] = {"ID": 0 }

ref: https://docs.python.org/3/tutorial/datastructures.html

Regarding the file writing, you might want to serialize your object usgin JSON before while it :

fd = open("my_list.json","w")
json.dump(my_list, fd)

Upvotes: 1

Related Questions