首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有字典的Python OOPS类

带有字典的Python OOPS类
EN

Stack Overflow用户
提问于 2018-07-31 07:11:27
回答 1查看 130关注 0票数 0

数据结构

代码语言:javascript
复制
myshelf = {}
myshelf["PY"] ={}
myshelf["PY"]["WWW"] = {"Name": "Book1", "Class": "5th"}
myshelf["PY"]["XXX"] = {"Name": "Book2", "Class": "6th"}
myshelf["EE"] ={}
myshelf["EE"]["YYY"] = {"Name": "Book3", "Class": "7th"}
myshelf["EE"]["ZZZ"] = {"Name": "Book4", "Class": "8th"}
print(myshelf) 
for shelfkey in myshelf.keys():
for subshelf in myshelf[shelfkey].keys():
    print(myshelf[shelfkey][subshelf])

下面是上面代码的输出,这意味着我的数据结构是正常的。

代码语言:javascript
复制
{'PY': {'WWW': {'Name': 'Book1', 'Class': '5th'}, 'XXX': {'Name': 'Book2', 'Class': '6th'}}, 'EE': {'YYY': {'Name': 'Book3', 'Class': '7th'}, 'ZZZ': {'Name': 'Book4', 'Class': '8th'}}}
{'Name': 'Book1', 'Class': '5th'}
{'Name': 'Book2', 'Class': '6th'}
{'Name': 'Book3', 'Class': '7th'}
{'Name': 'Book4', 'Class': '8th'}

现在我有了一个包含书籍的csv文件,我想把所有的书都放到我的书架上。注意: myShelf有SubShelf。ShelfName是书名的2个字母的大写字母,我的子架有ISBN书号。(考虑到特定图书的ISBN编号将保持不变,并且永远不会更改)

CSV文件格式

代码语言:javascript
复制
1   A11B12C13D14    Python Expert   English India   Raj     500 2
2   A11B12C13D14    Python Expert   English India   Raj     500 2
3   A11B12C13D14    Python Expert   English India   Raj     500 2
4   A11B12C13D16    Python Advanced English USA Amit    40000   1
5   A11B12C13D17    Aws Arch    English USA Sumit   40000   1

这里是我的主要代码

代码语言:javascript
复制
import csv

class Book:

def __init__(self, isbn, name, language, origin, authors, price, version):
    self.isbn = isbn
    self.name = name
    self.language = language
    self.origin = origin
    self.authors = authors
    self.price = price
    self.version = version
    self.counter= 1


class Shelf:
    def __init__(self, mycsvfilepath):
        self.shelf = {}
        self.mycsvfilepath = mycsvfilepath


        def _add_books_(self):
    with open(self.mycsvfilepath, "rt") as my_csv_file:
        my_csv_reader = csv.reader(my_csv_file)
        for line in my_csv_reader:
            mykeyforshelf = line[2][0:2].upper()
            print("ShelfName ={}".format(mykeyforshelf))
            print("SubShelfName={}".format(line[1]))
            if mykeyforshelf not in self.shelf.keys():
                self.shelf[mykeyforshelf] = {}
                self.shelf[mykeyforshelf][line[1]] = Book(line[1],line[2],line[3],line[4],line[5],line[6],line[7])
                print("Added New Book to new shelf ={} , subshelf= {}, BookName= {}".format(mykeyforshelf,line[1],self.shelf[mykeyforshelf][line[1]].name))
                print(self.shelf)
                print("\n")
            elif line[1] in self.shelf[mykeyforshelf].keys():
                print("this is existing shelf = {} and subshelf={}".format(mykeyforshelf,line[1]))
                print("existing counter for this book = {}".format(self.shelf[mykeyforshelf][line[1]].counter))
                self.shelf[mykeyforshelf][line[1]].counter = self.shelf[mykeyforshelf][line[1]].counter + 1
                print("New counter for this book = {}".format(self.shelf[mykeyforshelf][line[1]].counter))
                print(self.shelf)
                print("\n")
            else:
                print("I am existing shelf = {} , But new Sub Shelf ={}".format(mykeyforshelf,line[1]))
                self.shelf[mykeyforshelf][line[1]] = Book(line[1],line[2],line[3],line[4],line[5],line[6],line[7])
                print(self.shelf)
                print("\n")


myShelfObj = Shelf("E:\\Ashish\MyCsvFiles\\myCurrentInventory.csv")
myShelfObj._add_books_()
print("_____________________________________________________________")
print(myShelfObj)
print(type(myShelfObj))

#for shelfkey in myShelfObj:
#    for subshelf in myShelfObj.shelfkey:
#        print(myShelfObj[shelfkey][subshelf])

如果我保持3行以上的哈希标记,myCode就是工作文件。下面是工作代码的输出。

代码语言:javascript
复制
ShelfName =PY
SubShelfName=A11B12C13D14
Added New Book to new shelf =PY , subshelf= A11B12C13D14, BookName= Python 
Expert
{'PY': {'A11B12C13D14': <__main__.Book object at 0x000000FC39A1C940>}}


ShelfName =PY
SubShelfName=A11B12C13D14
this is existing shelf = PY and subshelf=A11B12C13D14
existing counter for this book = 1
New counter for this book = 2
{'PY': {'A11B12C13D14': <__main__.Book object at 0x000000FC39A1C940>}}


ShelfName =PY
SubShelfName=A11B12C13D14
this is existing shelf = PY and subshelf=A11B12C13D14
existing counter for this book = 2
New counter for this book = 3
{'PY': {'A11B12C13D14': <__main__.Book object at 0x000000FC39A1C940>}}


ShelfName =PY
SubShelfName=A11B12C13D16
I am existing shelf = PY , But new Sub Shelf =A11B12C13D16
{'PY': {'A11B12C13D14': <__main__.Book object at 0x000000FC39A1C940>, 
'A11B12C13D16': <__main__.Book object at 0x000000FC39A1CA58>}}


ShelfName =AW
SubShelfName=A11B12C13D17
Added New Book to new shelf =AW , subshelf= A11B12C13D17, BookName= Aws Arch
{'PY': {'A11B12C13D14': <__main__.Book object at 0x000000FC39A1C940>, 
'A11B12C13D16': <__main__.Book object at 0x000000FC39A1CA58>}, 'AW': 
{'A11B12C13D17': <__main__.Book object at 0x000000FC39A1CAC8>}}


_____________________________________________________________________________
<__main__.Shelf object at 0x000000FC399C9748>
<class '__main__.Shelf'>

但正如我上面提到的,如果我解开最后3行,它会给出下面提到的错误:

代码语言:javascript
复制
for shelfkey in myShelfObj:
TypeError: 'Shelf' object is not iterable

请帮帮我。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51603428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档