python实现excel读写数据

 更新时间:2021年03月02日 09:53:45   作者:坏蛋是我  
这篇文章主要为大家详细介绍了python操作EXCEL读数据、写数据的实例源码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python操作EXCEL的实例源码,供大家参考,具体内容如下

读EXCEL的操作:把excel的数据存储为字典类型

#coding=utf8 
 
#导入读excel的操作库 
import xlrd 
class GenExceptData(object): 
 def __init__(self): 
  try: 
   self.dataDic={} 
   #打开工作薄 
   self.wkbook= xlrd.open_workbook("Requirement.xls") 
   #获取工作表“requirement” 
   self.dataSheet=self.wkbook.sheet_by_name("requirement") 
   #把数据按 按照相应格式写入excel表中 
   self.readDataToDicl() 
   #保存文件 
  except Exception,e: 
   print "Read Excel error:",e 
 
 def readDataToDicl(self): 
  try: 
   nrows = self.dataSheet.nrows 
   ncols = self.dataSheet.ncols 
    
   print ncols ,nrows 
   try: 
    for rowNum in range(1,nrows):   
       #把数据的当前行的元素与上一行元素作比较 
       #如果不相等执行if语句 
       try: 
         
        singleJson={} 
        propertyName=self.dataSheet.cell(rowNum,3).value 
        propertyValue=self.dataSheet.cell(rowNum,4).value 
               
        if self.dataSheet.cell(rowNum,0).value and self.dataSheet.cell(rowNum,2).value: 
         mdEvent=self.dataSheet.cell(rowNum,0).value 
         singleJson["serviceId"]=self.dataSheet.cell(rowNum,2).value 
        singleJson[propertyName]=propertyValue        
        print singleJson 
        self.dataDic[mdEvent]=singleJson 
        singleJson.clear() 
         
         
       except Exception,e: 
        print "Get Data Error:",e 
   except Exception,e: 
    print "Reading Data Error:",e 
  except Exception,e: 
   print "Reading Data TO Dic Error:",e 
 
  
     
   
def test(): 
 GenExceptData() 
  
if __name__=="__main__": 
 test() 

写EXCEL的操作:把csv文件的数据按照需求写入到excel文件中

#coding=utf8 
 
from readCSV import readCSV 
import xlwt 
 
class GenTestCase(): 
 def __init__(self,path="E:\\PythonDemo\\OperExcel\\Demo.csv"): 
  self.dataInfor=readCSV(path) 
  #创建工作薄 
  self.wkbook=xlwt.Workbook() 
  #创建表:“埋点需求” 
  self.dataSheet=self.wkbook.add_sheet("shellt") 
  self.creatHead() 
   
 def creatHead(self): 
  firstLine=[] 
  #创建表头 
  for index in range(len(firstLine)): 
   self.dataSheet.write(0,index,firstLine[index]) 
    
  dataBody=self.dataInfor.buffer 
  print dataBody.__len__() 
  currentrow=1 
  for rowNum in range(1,len(dataBody)): 
   for index in range(len(dataBody[rowNum])): 
    if rowNum>1: 
     if dataBody[rowNum-1][0]!=dataBody[rowNum][0] : 
      print currentrow,rowNum 
      if currentrow==1: 
       for cols in range(3): 
        cellValue=dataBody[currentrow][cols] 
        cellValue=cellValue.decode("gbk") 
        data=u"%s" %(cellValue) 
        self.dataSheet.write_merge(currentrow,rowNum-1,cols,cols,data) 
         
       for cols in range(6,13): 
        cellValue=dataBody[currentrow][cols] 
        cellValue=cellValue.decode("gbk") 
        data=u"%s" %(cellValue) 
        self.dataSheet.write_merge(currentrow,rowNum-1,cols,cols,data)  
      else:  
       for cols in range(3): 
        cellValue=dataBody[currentrow][cols] 
        cellValue=cellValue.decode("gbk") 
        data=u"%s" %(cellValue) 
        self.dataSheet.write_merge(currentrow-1,rowNum-1,cols,cols,data) 
       for cols in range(6,12): 
        cellValue=dataBody[currentrow][cols] 
        cellValue=cellValue.decode("gbk") 
        data=u"%s" %(cellValue) 
        self.dataSheet.write_merge(currentrow-1,rowNum-1,cols,cols,data)            
      currentrow=rowNum+1       
     break 
    
   for cols in range(3,6):    
    cellValue=dataBody[rowNum][cols] 
    cellValue=cellValue.decode("gbk") 
    data=u"%s" %(cellValue) 
    self.dataSheet.write(rowNum,cols,data) 
         
  self.wkbook.save(r'reqq.xlsx') 
  
     
   
def test(): 
 GenTestCase() 
  
if __name__=="__main__": 
 test() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • python自动重试第三方包retrying模块的方法

    python自动重试第三方包retrying模块的方法

    retrying是一个python的重试包,可以用来自动重试一些可能运行失败的程序段。这篇文章主要介绍了python自动重试第三方包retrying的方法,需要的朋友参考下吧
    2018-04-04
  • Python中BaseHTTPRequestHandler实现简单的API接口

    Python中BaseHTTPRequestHandler实现简单的API接口

    本文主要介绍了Python中BaseHTTPRequestHandler实现简单的API接口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • python中pip的安装与使用教程

    python中pip的安装与使用教程

    在安装pip前,请确认win系统中已经安装好了python,和easy_install工具,下面脚本之家小编给大家详细介绍python中pip的安装与使用教程,感兴趣的朋友一起看看吧
    2018-08-08
  • python 远程执行命令的详细代码

    python 远程执行命令的详细代码

    有时会需要在远程的机器上执行一个命令,并获得其返回结果。对于这种情况,python 可以很容易的实现。今天通过实例代码介绍下python 远程执行命令的相关知识,感兴趣的朋友一起看看吧
    2022-02-02
  • 解决python -m pip install --upgrade pip 升级不成功问题

    解决python -m pip install --upgrade pip 升级不成功问题

    这篇文章主要介绍了python -m pip install --upgrade pip 解决升级不成功问题,需要的朋友可以参考下
    2020-03-03
  • Python显示进度条的方法

    Python显示进度条的方法

    这篇文章主要介绍了Python显示进度条的方法,以实例的形式进行了详细的分析,是一个非常实用的技巧,需要的朋友可以参考下
    2014-09-09
  • 详解Python如何优雅的重试

    详解Python如何优雅的重试

    这篇文章主要为大家介绍了Python如何优雅的重试详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Python Conda安装包报错:PackagesNotFoundError两种解决方法

    Python Conda安装包报错:PackagesNotFoundError两种解决方法

    这篇文章主要给大家介绍了关于Python Conda安装包报错:PackagesNotFoundError的两种解决方法,这通常意味着安装程序正在寻找的环境包没有在 conda 的默认通道中找到,文中将解决的办法介绍的非常详细,需要的朋友可以参考下
    2024-06-06
  • Python使用urllib模块对URL网址中的中文编码与解码实例详解

    Python使用urllib模块对URL网址中的中文编码与解码实例详解

    这篇文章主要介绍了Python使用urllib模块对URL网址中的中文编码与解码(Python使用urllib模块对URL编码与解码)实例详解,需要的朋友可以参考下
    2020-02-02
  • 如何使用Python破解ZIP或RAR压缩文件密码

    如何使用Python破解ZIP或RAR压缩文件密码

    这篇文章主要介绍了如何使用Python破解ZIP或RAR压缩文件密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01

最新评论