python读取csv大数据文件到mysql数据库中(ubunu14.04下)
2015-03-29 23:21
211 查看
#- * - coding: UTF-8 -*-
import mysql.connector
import csv
#数据库连接
config={
'host':'127.0.0.1',#默认127.0.0.1
'user':'root',
'password':'ict',
'port':3306 ,#默认即为3306
'database':'tianyi',
'charset':'utf8'#默认即为utf8
}
try:
cnn=mysql.connector.connect(**config)
if cnn:
print 'ok'
except mysql.connector.Error as e:
print('connect fails!{}'.format(e))
#处理
cursor=cnn.cursor()
#插入数据
#读取用户csv的文件
read = csv.reader(open('tianchi_mobile_recommend_train_user.csv'))
count = 0
datas = []
for useid,idtem,behavior,ugeohash,cate,time in read:
sql_insert="insert into tian_yi_user(user_id,item_id,behavior_type,user_geohash,item_category,time) values (%(user_id)s,%(item_id)s,%(behavior_type)s,%(user_geohash)s,%(item_category)s,%(time)s)"
data = {'user_id':useid,'item_id':idtem,'behavior_type':behavior,'user_geohash':ugeohash,'item_category':cate,'time':time}
datas.append(data)
count+=1
if count/200000==1:
cursor.executemany(sql_insert,datas)
cnn.commit()
count=0
datas=[]
cursor.executemany(sql_insert,datas)
cnn.commit()
cursor.close()
cnn.close()
#读取商品csv的文件
read = csv.reader(open('tianchi_mobile_recommend_train_item.csv'))
for idtem,geohash,category in read:
sql_insert="insert into tian_yi_item(item_id,item_geohash,item_category) values (%(item_id)s,%(item_geohash)s,%(item_category)s)"
data = {'item_id':idtem,'item_geohash':geohash,'item_category':category}
cursor.execute(sql_insert,data)
cnn.commit()
cursor.close()
cnn.close()
读取文件数据时注释掉其中一个。
import mysql.connector
import csv
#数据库连接
config={
'host':'127.0.0.1',#默认127.0.0.1
'user':'root',
'password':'ict',
'port':3306 ,#默认即为3306
'database':'tianyi',
'charset':'utf8'#默认即为utf8
}
try:
cnn=mysql.connector.connect(**config)
if cnn:
print 'ok'
except mysql.connector.Error as e:
print('connect fails!{}'.format(e))
#处理
cursor=cnn.cursor()
#插入数据
#读取用户csv的文件
read = csv.reader(open('tianchi_mobile_recommend_train_user.csv'))
count = 0
datas = []
for useid,idtem,behavior,ugeohash,cate,time in read:
sql_insert="insert into tian_yi_user(user_id,item_id,behavior_type,user_geohash,item_category,time) values (%(user_id)s,%(item_id)s,%(behavior_type)s,%(user_geohash)s,%(item_category)s,%(time)s)"
data = {'user_id':useid,'item_id':idtem,'behavior_type':behavior,'user_geohash':ugeohash,'item_category':cate,'time':time}
datas.append(data)
count+=1
if count/200000==1:
cursor.executemany(sql_insert,datas)
cnn.commit()
count=0
datas=[]
cursor.executemany(sql_insert,datas)
cnn.commit()
cursor.close()
cnn.close()
#读取商品csv的文件
read = csv.reader(open('tianchi_mobile_recommend_train_item.csv'))
for idtem,geohash,category in read:
sql_insert="insert into tian_yi_item(item_id,item_geohash,item_category) values (%(item_id)s,%(item_geohash)s,%(item_category)s)"
data = {'item_id':idtem,'item_geohash':geohash,'item_category':category}
cursor.execute(sql_insert,data)
cnn.commit()
cursor.close()
cnn.close()
读取文件数据时注释掉其中一个。
相关文章推荐
- Python webdriver 读取本地csv文件中数据 提示:IOError: [Errno 2] No such file or directory
- 使用python的pandas库读取csv文件保存至mysql数据库
- python的读取csv文件数据
- python通过串口读取GPS NMEA格式的数据,并保存为csv文件
- ean13码的生成,python读取csv中数据并处理返回并写入到另一个csv文件中
- Python使用pandas & pymysql读取MySQL数据到csv文件中
- Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法
- python3将csv文件中的两列数据读取出来,并且按行写入txt文本之中
- 使用python读取csv文件,并将数据更新至mysql
- java将float数据写入csv 并在python中读取csv文件
- 使用python pandas读取csv文件数据
- python 读取.csv文件数据到数组(矩阵)的实例讲解
- 数据保存!!!Python 爬取网页数据后,三种保存格式---保存为txt文件、CSV文件和mysql数据库
- Python导出数据到Excel可读取的CSV文件的方法
- python 读取.csv文件数据到数组(矩阵)
- Python导出数据到Excel可读取的CSV文件的方法
- 读书笔记--python数据可视化--001_读取CSV文件数据
- Selenium(Python) ddt读取CSV文件数据驱动
- Python脚本---把MySQL数据库表中的数据导出生成csv格式文件
- Java从.CSV文件中读取数据和写入