python实现将txt文件内容存入mysql数据库中

import pymysql
#连接数据库
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
passwd='123456',
db='test',
charset='utf8',
)
cursor = conn.cursor()
f = open('C:\\Users\\zyl\\Desktop\\新建文件夹\\shangweiwenjian.txt', "r",encoding = 'utf-8')
while True:
#逐行读取
line = f.readlines()
if line:
#处理每行\n
line = "".join(line)
line = line.strip('\n')
line = line.split(",")
content = line[0]
cursor.execute(
"insert into article(content_detail) values(%s)",
[content])
else:
break
 
f.close()
cursor.close()
conn.commit()
conn.close()
posted @ 2019-08-02 08:45  orangeYY  阅读(9355)  评论(0编辑  收藏  举报