3

高速アクセスのためにデータベースに保存し、辞書への入力に伴う計算時間をスキップしたい大きな辞書 (366MB の文字列として出力、〜 383764153 行 filetextfile) があります。

私の辞書は、ファイル名とコンテンツのペアの辞書の辞書で構成されています。小さなサブセット:

{
    'Reuters/19960916': {
        '54826newsML': '<?xml version="1.0"
encoding="iso-8859-1" ?>\r\n<newsitem itemid="54826" id="root"
date="1996-09-16" xml:lang="en">\r\n<title>USA: RESEARCH ALERT -
Crestar Financial cut.</title>\r\n<headline>RESEARCH ALERT - Crestar
Financial cut.</headline>\r\n<text>\n<p>-- Salomon Brothers analyst
Carole Berger said she cut her rating on Crestar Financial Corp to
hold from buy, at the same time lowering her 1997 earnings per share
view to $5.40 from $5.85.</p>\n<p>-- Crestar said it would buy
Citizens Bancorp in a $774 million stock swap.</p>\n<p>-- Crestar
shares were down 2-1/2 at 58-7/8. Citizens Bancorp soared 14-5/8 to
46-7/8.</p>\n</text>\r\n<copyright>(c) Reuters Limited',
        '55964newsML': '<?xml version="1.0" encoding="iso-8859-1"
?>\r\n<newsitem itemid="55964" id="root" date="1996-09-16"
xml:lang="en">\r\n<title>USA: Nebraska cattle sales thin at
$114/dressed-feedlot.</title>\r\n'
    }
}

MongoDBが適していると思いましたが、キーと値の両方が Unicode である必要があり、ファイル名を取得しているため、Unicode であるとnamelist()ZipFile限りません。

この辞書をデータベースにシリアライズする方法を教えてください。

4

2 に答える 2

5

pymongo は文字列が Unicode である必要はありません。実際には ascii 文字列をそのまま送信し、Unicode を UTF8 にエンコードします。pymongo からデータを取得するときは、常に unicode を取得します。@@ http://api.mongodb.org/python/2.0/tutorial.html#a-note-on-unicode-strings

入力に上位バイト ( など) を持つ「国際的な」バイト文字列が含まれている場合、ab\xC3cdこれらの文字列を Unicode に変換するか、UTF-8 としてエンコードする必要があります。以下は、任意のネストされた dict を処理する単純な再帰コンバーターです。

def unicode_all(s):
    if isinstance(s, dict):
        return dict((unicode(k), unicode_all(v)) for k, v in s.items())
    if isinstance(s, list):
        return [unicode_all(v) for v in s]
    return unicode(s)
于 2012-06-06T18:59:23.517 に答える
0

RAMを持っている場合(そして、最初に辞書にデータを入力したので、明らかに持っています)- cPickle。または、必要なRAMが少ないが、速度が遅くなるものが必要な場合- shelve

于 2012-06-06T18:40:46.117 に答える