所以我有一些代码使用lxml包从yahoo财务中删除一些关于财务数据的html,并对其进行各种操作。我创建了一个包含我需要的所有相关信息的字典,并且我一直在尝试使用pickle保存对象,因为每次创建它需要大约5分钟。但是,当我尝试使用以下代码行时,我在上面的标题中得到错误:
file = open('file.obj', 'w')
pickle.dump(headline_dictionary, file)
由于我对pickle的经验不多,我认为问题来自于lxml包创建的ElementBase对象无法被pickle。如果是这种情况,是否有其他方法可以保存我的对象,或者我是否以错误的方式使用泡菜?
提前致谢!
答案 0 :(得分:0)
I ran into the same problem as you. To persist your dictionary as a pickled object, you'll first need to cast the dictionary's values (aka data scraped through lxml) as strings or integers for example. That's your workaround.
Example code, where value has been scraped through lxml:
dict["key"] = str(value)