【发布时间】:2011-10-18 02:52:47
【问题描述】:
为什么你有一个包含数字和其他类型对象的列表?您似乎在尝试弥补设计缺陷。
事实上,我希望它以这种方式工作,因为我想保留已经在 JsonedData() 中编码的数据,然后我希望 json 模块给我一些方法来插入“原始”项目数据而不是默认值,以便编码的 JsonedData 可以重复使用。
这是代码,谢谢
import json
import io
class JsonedData():
def __init__(self, data):
self.data = data
def main():
try:
for chunk in json.JSONEncoder().iterencode([1,2,3,JsonedData(u'4'),5]):
print chunk
except TypeError: pass# except come method to make the print continue
# so that printed data is something like:
# [1
# ,2
# ,3
# ,
# ,5]
【问题讨论】:
-
你为什么不关心 TypeError?你至少不想看看哪里出了问题吗?
-
为什么你有一个包含数字和其他类型对象的列表?您似乎在尝试弥补设计缺陷。