添加数据库写入功能
This commit is contained in:
parent
0f44411114
commit
db6bd39356
5
ml/div.py
Normal file
5
ml/div.py
Normal file
@ -0,0 +1,5 @@
|
||||
#encoding=utf-8
|
||||
import jieba
|
||||
seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
|
||||
for l in seg_list:
|
||||
print l
|
21
news_spider/news2db.py
Normal file
21
news_spider/news2db.py
Normal file
@ -0,0 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#!/usr/bin/python
|
||||
import json
|
||||
import sqlite3
|
||||
import sys
|
||||
reload(sys)
|
||||
sys.setdefaultencoding('utf-8')
|
||||
|
||||
file = open('title.json')
|
||||
conn = sqlite3.connect('news.db')
|
||||
while 1:
|
||||
line = file.readline()
|
||||
if not line:
|
||||
break
|
||||
data = json.loads(line)
|
||||
insertsql = "insert into news(title,time,url) values ('"+str(data['title']).decode('utf-8')+"','"+str(data['time']).decode('utf-8')+"','"+str(data['url']).decode('utf-8')+"')"
|
||||
print insertsql
|
||||
conn.execute(insertsql)
|
||||
conn.commit()
|
||||
|
||||
conn.close()
|
Loading…
Reference in New Issue
Block a user