
你好,csv格式的和Excel格式的都是差不多的,
下面是读取Excel的一些函数,希望帮到你:
# -- coding: cp936 --import xlrd3
def getAllRowsBySheetIndex(sheetIndex, xlsFilePath):
workBook = xlrd3open_workbook(xlsFilePath)
table = workBooksheets()[sheetIndex]
rows = []
rowNum = tablenrows # 总共行数
rowList = tablerow_values
for i in range(rowNum):
rowsappend(rowList(i)) # 等价于rowsappend(i, rowLists(i))
return rows
def getRow(sheetIndex, rowIndex, xlsFilePath):
rows = getAllRowsBySheetIndex(sheetIndex, xlsFilePath)
return rows[rowIndex]
def getAllColsBySheetIndex(sheetIndex, xlsFilePath):
workBook = xlrd3open_workbook(xlsFilePath)
table = workBooksheets()[sheetIndex]
cols = []
colNum = tablencols # 总共列数
colList = tablecol_values
for i in range(colNum):
colsappend(colList(i))
return cols
def getCol(sheetIndex, colIndex, xlsFilePath):
cols = getAllColsBySheetIndex(sheetIndex, xlsFilePath)
return cols[colIndex]
def getCellValue(sheetIndex, rowIndex, colIndex, xlsFilePath):
workBook = xlrd3open_workbook(xlsFilePath)
table = workBooksheets()[sheetIndex]
return tablecell(rowIndex, colIndex)value # 或者tablerow(0)[0]value或者tablecol(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第四行第二列的单元格的值
这个脚本可以直接运行,将csv文件放在同级目录即可。
csv第一列需要有列名,如果csv里没有列名,需要在代码中添加列名。
代码运行示例:python insertpy csvname tablename
以上就是关于python 怎么实现从csv文件中读取数据 插入到mysql数据库中全部的内容,包括:python 怎么实现从csv文件中读取数据 插入到mysql数据库中、用python提取csv文件内容到数据库、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)