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()

     

  • 相关阅读:
    NOIP2008双栈排序[二分图染色|栈|DP]
    洛谷P1108 低价购买[DP | LIS方案数]
    洛谷P1330封锁阳光大学[二分图染色]
    NOIP模板整理计划
    期中考试
    UVA 10564 Paths through the Hourglass[DP 打印]
    UVA 11404 Palindromic Subsequence[DP LCS 打印]
    POJ2479 Maximum sum[DP|最大子段和]
    POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
    UVA11324 The Largest Clique[强连通分量 缩点 DP]
  • 原文地址:https://www.cnblogs.com/tangda/p/12180334.html
Copyright © 2011-2022 走看看