我正在尝试检索已放入字典的对象,但每次尝试检索它时都会收到错误消息:
class CSVEntry:
lat = []
lon = []
count = 0
# Create dictionary for tracking inputs
dict = defaultdict(list)
# Lookup the zipcode (returns an integer value)
zipcode = ConvertLatLonToZip(row[latCol], row[lonCol])
# If the zipcode is already in the dictionary, update it with the new count
if zipcode in dict:
oldEntry = dict[zipcode]
oldEntry.lat.append(row[latCol])
oldEntry.lon.append(row[lonCol])
oldEntry.count = dict[zipcode].count + 1
# Otherwise, make a new entry
else:
entry = CSVEntry()
entry.lat.append(row[latCol])
entry.lon.append(row[lonCol])
entry.count = 1
# Hash on the zipcode
dict[zipcode].append(entry)
将条目插入字典没有问题,但是一旦找到重复项,它就会失败并出现以下错误:
Traceback (most recent call last):
File "ParseCSV.py", line 125, in <module>
oldEntry.lat.append(row[latCol])
AttributeError: 'list' object has no attribute 'lat'
如果这是一个重复的问题或一个非常简单的问题,我深表歉意。我是 Python 的初学者,在决定发布之前我搜索了很长时间。
编辑:添加了 dict 的定义