gpt4 book ai didi

Python CouchDB 无法保存从 feedparser 条目创建的字典? (没有属性 'read' )

转载 作者:太空宇宙 更新时间:2023-11-03 12:51:28 25 4
gpt4 key购买 nike

我有一个脚本,我想读取 RSS 提要中的条目并将各个条目以 JSON 格式存储到 CouchDB 数据库中。

我的代码中有趣的部分看起来像这样:

Feed = namedtuple('Feed', ['name', 'url'])

couch = couchdb.Server(COUCH_HOST)
couch.resource.credentials = (COUCH_USER, COUCH_PASS)

db = couch['raw_entries']

for feed in map(Feed._make, csv.reader(open("feeds.csv", "rb"))):
d = feedparser.parse(feed.url)
for item in d.entries:
db.save(item)

当我尝试运行该代码时,我从 db.save(item) 中收到以下错误:

AttributeError: object has no attribute 'read'

好的,然后我做了一些调试......

for feed in map(Feed._make, csv.reader(open("feeds.csv", "rb"))):
d = feedparser.parse(feed.url)
for item in d.entries:
print(type(item))

结果为 <class 'feedparser.FeedParserDict'> -- 啊,所以 feedparser 使用它自己的 dict 类型......好吧,如果我尝试将它显式转换为 dict 会怎么样? ?

for feed in map(Feed._make, csv.reader(open("feeds.csv", "rb"))):
d = feedparser.parse(feed.url)
for item in d.entries:
db.save(dict(item))

Traceback (most recent call last):
File "./feedchomper.py", line 32, in <module>
db.save(dict(item))
File "/home/dealpref/lib/python2.7/couchdb/client.py", line 407, in save
_, _, data = func(body=doc, **options)
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 399, in post_json
status, headers, data = self.post(*a, **k)
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 381, in post
**params)
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 419, in _request
credentials=self.credentials)
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 239, in request
resp = _try_request_with_retries(iter(self.retry_delays))
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 196, in _try_request_with_retries
return _try_request()
File "/home/dealpref/lib/python2.7/couchdb/http.py", line 222, in _try_request
chunk = body.read(CHUNK_SIZE)
AttributeError: 'dict' object has no attribute 'read'

什么?这没有意义,因为以下工作正常并且类型仍然是 dict :

some_dict = dict({'foo': 'bar'})
print(type(some_dict))
db.save(some_dict)

我在这里错过了什么?

最佳答案

我找到了一种方法,将结构序列化为 JSON,然后返回到我传递给 CouchDB 的 Python dict —— 然后将其重新序列化回 JSON 以保存(是的,很奇怪而且不太受欢迎,但它有效吗?)

我必须为转储执行自定义序列化程序方法,因为 time_structrepr 不能被eval

来源:http://diveintopython3.org/serializing.html

代码:

#!/usr/bin/env python2.7

from collections import namedtuple
import csv
import json
import time

import feedparser
import couchdb

def to_json(python_object):
if isinstance(python_object, time.struct_time):
return {'__class__': 'time.asctime',
'__value__': time.asctime(python_object)}

raise TypeError(repr(python_object) + ' is not JSON serializable')

Feed = namedtuple('Feed', ['name', 'url'])

COUCH_HOST = 'http://mycouch.com'
COUCH_USER = 'user'
COUCH_PASS = 'pass'

couch = couchdb.Server(COUCH_HOST)
couch.resource.credentials = (COUCH_USER, COUCH_PASS)

db = couch['raw_entries']

for feed in map(Feed._make, csv.reader(open("feeds.csv", "rb"))):
d = feedparser.parse(feed.url)
for item in d.entries:
j = json.dumps(item, default=to_json)
db.save(json.loads(j))

关于Python CouchDB 无法保存从 feedparser 条目创建的字典? (没有属性 'read' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5505667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com