zoukankan      html  css  js  c++  java
  • Python利用Psycopg2模块将Excel表格数据导入Postgressql

    1 切记踩坑点

    error_tup = () 应该为元组,而不是字典

    因为字典的value是可变类型,for循环时,列表添加字典时,如果在for 循环中对字典的值进行修改,此时列表中添加的所有的字典的值都被修改成相同的值了,即字典的值的地址可以修改,故把列表中所有的字典的值修改了,所以要用元祖不可变类型的值来替换字典。

    此时的结构应该是[(),(),(),()...]

     

     1 import xlrd
     2 import psycopg2
     3 import redis
     4 conn = redis.Redis(host='localhost', port=6379)
     5 pipe = conn.pipeline()
     6 def check_data():
     7     book = xlrd.open_workbook('test.xlsx')
     8     sheet = book.sheet_by_name("Sheet1")
     9     conn = psycopg2.connect(dbname="ggg",
    10                             user='postgres',
    11                             password='521314',
    12                             host="localhost",
    13                             port=5432)
    14     count = 0
    15     cur = conn.cursor()
    16     error_tup = ()
    17     error_list = []
    18     for r in range(1, sheet.nrows):
    19         name = sheet.cell(r, 0).value
    20         email = sheet.cell(r, 1).value
    21         data = (re.match(r'^[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+){0,4}@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+){0,4}$', email)) and 1 or 0
    22         if not ((email and data) and name):
    23             count += 1
    24             error_tup = (name, email)
    25             error_list.append(error_tup)
    26             continue
    27         pipe.hset('name', name, email)
    28     if not count:
    29         values = []
    30         for i in pipe.hget("name", name).command_stack:
    31             try:
    32                 value = (i[0][2], i[0][3])
    33                 values.append(value)
    34             except Exception:
    35                 pass
    36         query = "insert into users_employees (name, email) values(%s,%s);"
    37         # 批量提交数据 单条数据提交用cur.execute(query, value)
    38         cur.executemany(query, values)
    39         conn.commit()
    40         values.clear()
    41         conn.close()
    42         cur.close()
    43         columns = str(sheet.ncols)
    44         rows = str(sheet.nrows)
    45         print("{}行数据错误".format(count))
    46         print("导入" + columns + "" + rows + "行数据到pgsql数据库!!!")
    47         return "{}行数据错误".format(count)
    48     print(error_list[:20])
    49     return error_list[:20]
    50 
    51 
    52 if __name__ == '__main__':
    53     check_data()

     

  • 相关阅读:
    当有触发器时,涉及触发器的列名不能再随便更改了,因为改变列名时并没有改变触发器,而使触发器不会发生作用
    PHP实现上次登录功能
    TRUNCATE 不能引发触发器
    unslider点导航不显示错误
    jquery插件中使用ajax并且获取使用插件的对象
    jquery插件函数传参错误
    jquery插件获取事件类型
    线程安全的 stack
    不要在锁的作用域之外通过指针或引用传递要保护的数据
    通过打包 accumulate 实现多线程版本的 accumulate
  • 原文地址:https://www.cnblogs.com/tangda/p/12180334.html
Copyright © 2011-2022 走看看