#-*- 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