python读取txt里面的数据,拼接sql导入对应表中

import pymysql
'''该脚本功能是读取txt里面的数据,拼接sql导入对应表中'''
def read_txt(file_txt):
print("'''''''读取txt数据''''''")
fr = open(file_txt,'r',encoding='utf-8')
for line in fr.readlines():
row = line.strip().split("\t")
print(row)
insert_sql(row)
fr.close()

def insert_sql(row):
conn = pymysql.connect(
host="100.100.100.708",
port=3306,
user="test",
passwd="123",
db="bigdata",
charset="utf8"
)
cur = conn.cursor()
sql = "insert into ts_vehicle_brand (brand,total_count,vehicle_innet_count,vehicle_online_count,date) VALUES ('"+row[0]+"','"+row[1]+"','"+row[2]+"','"+row[3]+"','"+row[4]+"');"
print(sql)
cur.execute(sql)
conn.commit()
cur.close()
conn.close()

if __name__ == "__main__":
file_txt = r"E:\Users\admin\PycharmProjects\untitled\e"
read_txt(file_txt)
posted @ 2019-06-12 15:17  暮色森森  阅读(1067)  评论(0编辑  收藏  举报