免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
python

一   excel表格自動(dòng)將數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,限制文件后綴名為.xlsx

文件1:excelTodatabase.py
注意:每個(gè)sheet生成一張表,表名字為sheet的名字,自動(dòng)將int類型和日期類型轉(zhuǎn)成字符類型,數(shù)據(jù)庫連接方式改成自己對應(yīng)的數(shù)據(jù)庫,我這里是postgresql
  1. import xlrd
  2. from datetime import datetime
  3. from xlrd import xldate_as_tuple
  4. #根據(jù)有多少個(gè)sheets去創(chuàng)建多少個(gè)表,path為excel表格的路徑
  5. def createtable(path):
  6. # 讀取excel
  7. data = xlrd.open_workbook(path)
  8. # 根據(jù)sheet索引獲取sheet的內(nèi)容
  9. print("excel全部的sheet為:", data.sheet_names())
  10. sheet_names = data.sheet_names()
  11. table_one = data.sheet_by_index(0)
  12. print("一個(gè)sheet的全部列名為", table_one.row_values(0))
  13. conn = psycopg2.connect(database='test', user='postgres', password='root', host='localhost')
  14. cur = conn.cursor()
  15. for i in range(0, len(sheet_names)):
  16. #當(dāng)前sheet的名字
  17. table_name = sheet_names[i]
  18. # 當(dāng)前的sheet
  19. now_table = data.sheet_by_index(i)
  20. # 獲得當(dāng)前sheet的列數(shù)就是 屬性數(shù)
  21. cols_num = now_table.ncols
  22. # 獲得當(dāng)前表格的行數(shù),就是有多少的數(shù)據(jù)量
  23. rows_numn = now_table.nrows
  24. # 獲得當(dāng)前的屬性的數(shù)組,其實(shí)就是第一例的值
  25. attrs = now_table.row_values(0)
  26. #判斷表格是否存在
  27. cur.execute("SELECT to_regclass('%s') is not null" % table_name)
  28. flag = cur.fetchone()[0]
  29. print('flag',flag)
  30. if flag :
  31. print('存在了,直接將表的內(nèi)容插入')
  32. # 將當(dāng)前的sheet插入到數(shù)據(jù)庫
  33. for k in range(1, rows_numn):
  34. row_vlaue = now_table.row_values(k)
  35. print(row_vlaue)
  36. print(','.join(attrs))
  37. # 處理要插入的數(shù)據(jù),把非字符串的數(shù)據(jù)轉(zhuǎn)換成字符串類型,同事將字符串變成 sql語句需要的類型
  38. for a in range(0, len(row_vlaue)):
  39. ctype = now_table.cell(k, a).ctype
  40. print('ctype', ctype)
  41. #ctype: 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
  42. if ctype ==2 and row_vlaue[a] % 1 ==0 :
  43. tmp = int(row_vlaue[a])
  44. row_vlaue[a] = str(tmp)
  45. if ctype == 3 :
  46. d = datetime(*xldate_as_tuple(row_vlaue[a],0))
  47. row_vlaue[a] = d.strftime('%Y-%m-%d')
  48. c = row_vlaue[a]
  49. row_vlaue[a] = "'" + c + "'"
  50. print(','.join(row_vlaue))
  51. sql = "INSERT INTO %s(%s) VALUES(%s)" % (table_name, ','.join(attrs), ','.join(row_vlaue))
  52. print(sql)
  53. cur.execute(sql)
  54. conn.commit()
  55. else:
  56. cur.execute("CREATE TABLE " + table_name + "();")
  57. conn.commit()
  58. # 為sheet進(jìn)行建表,
  59. cur.execute("ALTER TABLE %s ADD COLUMN id SERIAL primary key ;" % table_name)
  60. conn.commit()
  61. # cur.execute("CREATE SEQUENCE users_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;" )
  62. # conn.commit()
  63. cur.execute("alter table %s alter column id set default nextval('users_id_seq'); " % table_name)
  64. conn.commit()
  65. for j in range(0, cols_num):
  66. cur.execute("ALTER TABLE %s ADD COLUMN %s VARCHAR(200);" % (table_name, attrs[j]))
  67. conn.commit()
  68. # 將當(dāng)前的sheet插入到數(shù)據(jù)庫
  69. for k in range(1, rows_numn):
  70. row_vlaue = now_table.row_values(k)
  71. print(row_vlaue)
  72. print(','.join(attrs))
  73. # 處理要插入的數(shù)據(jù),把非字符串的數(shù)據(jù)轉(zhuǎn)換成字符串類型,同事將字符串變成 sql語句需要的類型
  74. for a in range(0, len(row_vlaue)):
  75. ctype = now_table.cell(k, a).ctype
  76. print('ctype', ctype)
  77. # ctype: 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
  78. if ctype == 2 and row_vlaue[a] % 1 == 0:
  79. tmp = int(row_vlaue[a])
  80. row_vlaue[a] = str(tmp)
  81. if ctype == 3:
  82. d = datetime(*xldate_as_tuple(row_vlaue[a], 0))
  83. row_vlaue[a] = d.strftime('%Y-%m-%d')
  84. c = row_vlaue[a]
  85. row_vlaue[a] = "'" + c + "'"
  86. print(','.join(row_vlaue))
  87. sql = "INSERT INTO %s(%s) VALUES(%s)" % (table_name, ','.join(attrs), ','.join(row_vlaue))
  88. print(sql)
  89. cur.execute(sql)
  90. conn.commit()
  91. conn.close()

二   數(shù)據(jù)庫信息導(dǎo)出到excel表格,生成 .xls格式的表格(.xlsx格式打不開)

文件1 dbToExcel.py  

注意:修改對應(yīng)數(shù)據(jù)庫的連接方式,我這里是postgresql,返回結(jié)果"ok"代表成功

  1. import xlwt
  2. import psycopg2
  3. import os
  4. import datetime
  5. def tableExportToXlsx(sql):#sql 為數(shù)據(jù)庫查詢語句,將會(huì)把查詢的數(shù)據(jù)導(dǎo)出
  6. table_name = "acts"
  7. conn = psycopg2.connect(database='test',user='postgres',password='root',host='localhost')
  8. cur = conn.cursor()
  9. cur.execute(sql)
  10. #重置游標(biāo)位置
  11. cur.scroll(0,mode='absolute')
  12. #搜取所有的結(jié)果
  13. results = cur.fetchall()
  14. #獲取屬性名
  15. attrs = cur.description
  16. workbook = xlwt.Workbook()
  17. sheet = workbook.add_sheet(table_name,cell_overwrite_ok=True)
  18. #寫入表格的屬性值
  19. for i in range(0,len(attrs)):
  20. sheet.write(0,i,attrs[i][0])
  21. print('表格屬性:',attrs[i][0])
  22. #將數(shù)據(jù)庫的數(shù)據(jù)導(dǎo)入表格
  23. row = 1
  24. col = 0
  25. for row in range(1,len(results)+1):
  26. print('寫',row,'行數(shù)據(jù)')
  27. for col in range(0,len(attrs)):
  28. sheet.write(row,col,results[row-1][col])
  29. print(results[row-1][col])
  30. nowpath = os.path.dirname(__file__)
  31. print("現(xiàn)在的目錄是" + nowpath)
  32. act_path = os.path.dirname(nowpath)
  33. app_path = os.path.dirname(act_path)
  34. file_path = app_path + '\\xlsx_tmp'
  35. export_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
  36. file_name = 'act-{0}.xls'.format(export_time)
  37. print('文件路徑為' +os.path.join(file_path,file_name))
  38. workbook.save(os.path.join(file_path,file_name))
  39. if os.path.isfile(os.path.join(file_path,file_name)):
  40. print('數(shù)據(jù)庫中成功導(dǎo)出數(shù)據(jù)')
  41. return {'path':file_path,'name':file_name}
  42. else:
  43. print('數(shù)據(jù)庫導(dǎo)出錯(cuò)誤')
  44. return 'error'



本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
selenium實(shí)現(xiàn)excel文件數(shù)據(jù)的讀、寫
Python Excel Word一秒制作百份合同
python從入門到實(shí)踐,文件讀寫與Excel操作
【python】 xlrd的使用
python操作讀取excel表格
python中使用xlrd、xlwt操作excel表格詳解
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服