zoukankan      html  css  js  c++  java
  • Python 爬取数据入库mysql

     1 # -*- enconding:etf-8 -*-
     2 import pymysql
     3 import os
     4 import time
     5 import re
     6 serveraddr="localhost"
     7 user="root"
     8 password="123456"
     9 databaseName="test"
    10 filename="./data/UNCL.csv"
    11 
    12 def create_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 def test():
    31     p1=r"^s{13}w.+|
    $"
    32     pattern=re.compile(p1)
    33     fr=open(filename)
    34     w2=open('./data/e.csv','a')
    35     for line in fr.readlines():
    36         # print(line)
    37         matcher=re.findall(pattern,line)
    38         # print(matcher)
    39         # print(type(matcher))
    40         for i in matcher:
    41             w2.write(i)
    42             # w2.write("
    ")
    43     fr.close()
    44     w2.close()
    45 
    46     
    47 if __name__=='__main__':
    48     test()
  • 相关阅读:
    TCP三次握手过程
    btree b+tree 的关系
    volatile和指令重排序
    事务一致性理解 事务ACID特性的完全解答
    JVM 详解
    java 并发 详解
    socker TCP UDP BIO NIO
    mysql 主从复制 配置
    身份证格式验证 方法
    分布式事务 XA 两段式事务 X/open CAP BASE 一次分清
  • 原文地址:https://www.cnblogs.com/smuxiaolei/p/7366563.html
Copyright © 2011-2022 走看看