看世界
 
昨日:篇  今日:篇   总帖:篇   会员:
admin
创始人Lv1   
txt--> mysql     
import re
import os
import MySQLdb

db = MySQLdb.connect("127.0.0.1","root","********","data" ,charset="utf8")
cursor = db.cursor()

path= "D:\\down\\loupan\\chengdu\\"
L1 = path + 'chenghua.txt'
L2 = path + 'gaoxin.txt'
# path= "D:\\down\\loupan\\test\\"
# L1 = path + 'L1.txt'
# L2 = path + 'L2.txt'
LS=[L1,L2]
for l in LS:
    for line in open(l):
        lines = line.split(',')
        pattern = re.compile(r',')  # 查找,
        result1 = pattern.findall(line)
    n = 1
    for ad in lines:
        sa =ad
        i = ad
        print ad        
        sql = "INSERT INTO test(text,title) VALUES ('%s','%s')" % (sa,i)        
        cursor.execute(sql)   
        db.commit() 
        n += 1
db.close()
print 'ok'


TXT 文件 只能 ',' 间隔,不能加 '', 结尾不要,
 0  已被阅读了19次  楼主 2018-10-09 21:16:56
回复列表

回复:txt--> mysql

LOOK官方站 联系站长
Powered by Look博客