数据结构
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])
这是上面代码的输出,这意味着我的数据结构还可以。
{'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个字母大写,而我的Subshelf具有书号的ISBN。 (考虑到特定书籍的ISBN号将保持不变,并且永远不会改变)
CSV文件格式
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
现在这是我的主要代码
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])
myCode是工作文件,如果我保持3行以上的哈希标记。这是工作代码的输出。
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行,则会给我以下提到的错误:
for shelfkey in myShelfObj:
TypeError: 'Shelf' object is not iterable
请帮助我。
答案 0 :(得分:1)
输入myShelfObj类型时,您将看到自己的样子
print(type(myShelfObj))
这是输出
<class '__main__.Shelf'>
因此,您尝试遍历不可能的对象。您将收到此类错误。您只能将for循环用于列表元组字典,而不能用于对象。希望我清楚