1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import json
import codecs
import MySQLdb
import _mysql
db = MySQLdb.connect(host='', user="",passwd="",db="", charset='utf8', use_unicode=True)
cur = db.cursor()
sql_request='insert ignore into tweet_location (geo_coordinates,id_tweet) Values (%s,%s)'
print 'hello'
f = codecs.open("new1.txt",'r','utf-8')
cpt = 0
for line in f:
if len(line)< 10:
continue
print line
line = line.replace('\n','')
status = json.loads(line)
if 'geo' in status.keys() and 'coordinates' in status['geo'].keys():
geo_coordinates = status['geo']['coordinates']
else:
geo_coordinates = '36.584467, 3.185335'
cur.execute(sql_request,(status['geo']['coordinates'],status['id_str']))
print '=================='
cpt += 1
if cpt > 100000:
break
db.commit()
cur.close()
db.close()
f.close() |
Partager