python数据入库_Python 爬取数据入库mysql

#-*- enconding:etf-8 -*-

2 importpymysql3 importos4 importtime5 importre6 serveraddr="localhost"

7 user="root"

8 password="123456"

9 databaseName="test"

10 filename="./data/UNCL.csv"

11

12 defcreate_table():13 #sep=os.sep

14 #tableName=raw_input(‘please input the table name which will be created:‘)

15 #classpath=raw_input(‘请输入需要遍历的路径:‘)

16

17 db=pymysql.connect(serveraddr,user,password,databaseName)18 cursor=db.cursor()19 cursor.execute("drop table if exists `tncl`")20 sql="""create table `tncl`(21 `tncl_id` varchar(25) not null,22 `tncl_tag` varchar(25) not null,23 `tncl_desc` varchar(255) not null,24 `tncl_note` varchar(1200) not null,25 primary key(`tncl_id`)26 ) engine=InnoDB default charset=utf8;"""

27

28 cursor.execute(sql)29 db.close()30 deftest():31 p1=r"^\s{13}\w.+|\n$"

32 pattern=re.compile(p1)33 fr=open(filename)34 w2=open(‘./data/e.csv‘,‘a‘)35 for line infr.readlines():36 #print(line)

37 matcher=re.findall(pattern,line)38 #print(matcher)

39 #print(type(matcher))

40 for i inmatcher:41 w2.write(i)42 #w2.write("\n")

43 fr.close()44 w2.close()45

46

47 if __name__==‘__main__‘:48 test()

Python 爬取数据入库mysql

标签:.exe   cursor   code   print   input   findall   mat   root   取数据

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:http://www.cnblogs.com/smuxiaolei/p/7366563.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值