(优发娱乐wwwyoufa8) 用Python编辑SQLite的时候,怎样在遍历数据的时候,对数据进行计数,并将计数个数更新在表中?

想用Python控制SQLite,遍历数据的时候,对相同的数据进行计数并放在SQL的表中。但是我用以下代码运行的时候,相同的数据并没有叠加计数,而是每个数据都在表里单独一行,计数列都是1。请指教

import sqlite3
import urllib.request url=http://www.pythonlearn.com/code/mbox.txt
data=urllib.request.urlopenurl.read.decode #print typedata conn=sqlite3.connectemail.sqlite
cur=conn.cursor cur.executeDROP TABLE COUNTS
cur.executeCREATE TABLE COUNTS org TEXT, count INTEGER
for line in data.split
: #print line if not line.startswithFrom: continue words=line.split #print words email=words[1] # print email word=email.split@ s=word[0] #print s cur.executeSELECT count FROM COUNTS WHERE org = ?,s, try: count=cur.fetchone[0] cur.executeUPDATE count=count+1 WHERE org = ?,s, except: cur.executeINSERT INTO COUNTS org,count VALUES ?,1,s, conn.commit sqlstr=SELECT org,count FROM COUNTS ORDER BY count DESC LIMIT 10
for row in cur.executesqlstr: print strrow[0],row[1] cur.close

# coding=utf-8 import requests
import collections url = http://www.pythonlearn.com/code/mbox.txt
data = requests.geturl.text emails = [_.split[1] for _ in data.split if _.startswithFrom]
print collections.Counteremails.most_common10 #如果你只想取人名,这里再split一下
emails = [_.split[1].split@[0] for _ in data.split if _.startswithFrom]
print collections.Counteremails.most_common10

发表评论

电子邮件地址不会被公开。 必填项已用*标注