python如何读取数据库文件_python怎么实现从csv文件中读取数据插入到mysql数据库中...

八一xiaobayi

TB10fDzdND1gK0jSZFsXXbldVXa-36-32.png

2020-06-09 15:04:07

你好,csv格式的和Excel格式的都是差不多的,

下面是读取Excel的一些函数,希望帮到你: # -*- coding: cp936 -*-

import xlrd3

def getAllRowsBySheetIndex(sheetIndex, xlsFilePath):

workBook = xlrd3.open_workbook(xlsFilePath)

table = workBook.sheets()[sheetIndex]

rows = []

rowNum = table.nrows # 总共行数

rowList = table.row_values

for i in range(rowNum):

rows.append(rowList(i)) # 等价于rows.append(i, rowLists(i))

return rows

def getRow(sheetIndex, rowIndex, xlsFilePath):

rows = getAllRowsBySheetIndex(sheetIndex, xlsFilePath)

return rows[rowIndex]

def getAllColsBySheetIndex(sheetIndex, xlsFilePath):

workBook = xlrd3.open_workbook(xlsFilePath)

table = workBook.sheets()[sheetIndex]

cols = []

colNum = table.ncols # 总共列数

colList = table.col_values

for i in range(colNum):

cols.append(colList(i))

return cols

def getCol(sheetIndex, colIndex, xlsFilePath):

cols = getAllColsBySheetIndex(sheetIndex, xlsFilePath)

return cols[colIndex]

def getCellValue(sheetIndex, rowIndex, colIndex, xlsFilePath):

workBook = xlrd3.open_workbook(xlsFilePath)

table = workBook.sheets()[sheetIndex]

return table.cell(rowIndex, colIndex).value # 或者table.row(0)[0].value或者table.col(0)[0].value

if __name__=='__main__':

rowsInFirstSheet = getAllRowsBySheetIndex(0, './产品.xls')

print(rowsInFirstSheet)

colsInFirstSheet = getAllColsBySheetIndex(0, './产品.xls')

print(colsInFirstSheet)

print(getRow(0, 0, './产品.xls')) # 获取第一个sheet第一行的数据

print(getCol(0, 0, './产品.xls')) # 获取第一个sheet第一列的数据

print(getCellValue(0, 3, 2, './产品.xls')) # 获取第一个sheet第四行第二列的单元格的值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值