1

I need to create a JSON class base called JCarOwner. It has to get attributes from its owner such as name, manufacture and year. I want to make it an unknown attribute:

def add_co (existing owner name, new_attribute , attribute content)

But I don't really know how. This is my code (corrected):

import json

class JCarOwner:

    CarOwner = []

    def __init__(self,name,manufacture=None,production_year=None):
        super(JCarOwner, self).__init__()
        self.CarOwner.append(json.dumps({"Owner_Name": name, "Car_Manufacture": manufacture, "Production_Year": production_year}))

    def COsave(filename):
        JCarOwner.CarOwner.dumps(filename,separators=(',', ':'))

    def COload(filename):
        JCarOwner.CarOwner.append(json.load(filename))

    def DispInfo(Car_Owner):

        for owner in JCarOwner.CarOwner:

            CO = json.load(owner)
            if CO[owner]["Owner_Name"]==Car_Owner:
                print(CO[owner])


jane = JCarOwner("Jane","Mazda",2016)
bob = JCarOwner("bob")     

JCarOwner.DispInfo(jane)

error:

AttributeError: 'str' object has no attribute 'read'
13
  • 1
    What is a "json class" ? Commented May 10, 2017 at 10:21
  • just a class compose of json file in a list Commented May 10, 2017 at 10:22
  • And what is an "unknown attribute"? Commented May 10, 2017 at 10:24
  • @Daniel Roseman something that the user want to add, if the json file had only name and car so the new will be price. Commented May 10, 2017 at 10:25
  • json is a text format (like say csv, xml, yaml etc), so the term "json class" doesn't make any sense. Commented May 10, 2017 at 10:32

1 Answer 1

1

Use self.CarOwner instead of plain CarOwner.

 def __init__(self,name,manufacture,production_year):
   super(JCarOwner, self).__init__()

   self.CarOwner.append(json.dumps({"Owner_Name": name, "Car_Manufacture": manufacture, "Production_Year": production_year}))

Please try to follow certain python conventions.

Sign up to request clarification or add additional context in comments.

15 Comments

I get this error: TypeError: object.__init__() takes no parameters
Sorry my bad. fixed it.
I get this error: AttributeError: 'dict' object has no attribute 'read'
which json library are you using? I'm assuming you are just importing default python json library. Try using 'json.loads' instead of 'json.load'.
I am using default Jason and tried loads and got: TypeError: the JSON object must be str, bytes or bytearray, not 'dict'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.