Database 将python生成器转换为数据库

Database 将python生成器转换为数据库,database,python-2.7,sqlite,generator,Database,Python 2.7,Sqlite,Generator,我想使用readline\u google\u store(它是一个生成器)创建一个记录数据库。我的代码如下: import sqlite3 import re from google_ngram_downloader import readline_google_store import time def Main(): try: start_time = time.time() p = re.compile(r'^[a-z]*$', re.IGNOR

我想使用
readline\u google\u store
(它是一个生成器)创建一个记录数据库。我的代码如下:

import sqlite3
import re
from google_ngram_downloader import readline_google_store
import time

def Main():
    try:
        start_time = time.time()
        p = re.compile(r'^[a-z]*$', re.IGNORECASE)
        el = 'abcdefghijklmnopqrstuvwxyz'
        # Open database connection
        con = sqlite3.connect('test.db')
        # create a class object
        cur = con.cursor()
        for l in el:
            fname, url, records = next(readline_google_store(ngram_len=1, indices=l))
            for r in records:
                #time.sleep(0.0001)
                if r.year >= 2000:
                    w = r.ngram.lower()
                    if p.match(w):
                        cur.execute('SELECT ngram, match_counts FROM Unigram WHERE ngram = ?', (w,))
                        results = cur.fetchone()
                        # print results
                        if not results: # or if results == None
                            cur.execute("INSERT INTO Unigram VALUES(?, ?);", (w, r.match_count))
                            con.commit()
                        else:
                            match_count_sum = results[1] + r.match_count
                            cur.execute("UPDATE Unigram SET match_counts = ? WHERE ngram = ?;", (match_count_sum, w))
                            con.commit()
    except sqlite3.Error, e:
        if con:
            con.rollback()
            print 'There was a problem with sql'
    finally:
        if con:
            con.close()
    end_time = time.time()
    print("--- It took %s seconds ---" % (end_time - start_time))

if __name__ == '__main__':
    Main() 
输入为以下格式的(记录):

(ngram, year, match_count, page_count)
不考虑年份和页数,我希望有一个记录如下的表:
(ngram,match\u count\u sum)
其中
match\u count\u sum
是不同年份的所有
match\u count
的总和

弹出的错误是:

requests.exceptions.ChunkedEncodingError:(“连接中断:错误(54,'对等方重置连接')”,错误(54,'对等方重置连接'))

我尝试了
time.sleep(0.0001)
来调整线程调度并允许套接字I/O完成,但是我得到了超时错误


如何解决此问题?

由于SQLite似乎正在本地读/写,您的错误似乎是远程API的问题。通常,这将是应用程序的缓慢部分,但我希望从中读取会被阻塞

对等端的连接重置通常表示某个地方出现网络错误。所以问题是重置是从哪里来的(可能是防火墙、API限制等。根据信息,不知道重置是从哪里来的,但我可以给你一个初步清单

  • 它是否总是达到相同的记录?另一端可能存在服务器端限制
  • 这是随机发生的吗?网络是否有问题,您只需要优雅地处理故障

  • 这超出了代码的明确控制范围,但您可能能够更优雅地处理故障。

    问题已解决:我使用了专为分层数据设计的magic pytable,而不是sql…了解更多信息:和