溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python如何實(shí)現(xiàn)excel讀寫數(shù)據(jù)

發(fā)布時(shí)間:2021-03-24 10:17:22 來源:億速云 閱讀:106 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹python如何實(shí)現(xiàn)excel讀寫數(shù)據(jù),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

讀EXCEL的操作:把excel的數(shù)據(jù)存儲(chǔ)為字典類型

#coding=utf8 
 
#導(dǎo)入讀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") 
      #把數(shù)據(jù)按 按照相應(yīng)格式寫入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):     
              #把數(shù)據(jù)的當(dāng)前行的元素與上一行元素作比較 
              #如果不相等執(zhí)行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文件的數(shù)據(jù)按照需求寫入到excel文件中

#coding=utf8 
 
from readCSV import readCSV 
import xlwt 
 
class GenTestCase(): 
  def __init__(self,path="E:\\PythonDemo\\OperExcel\\Demo.csv"): 
    self.dataInfor=readCSV(path) 
    #創(chuàng)建工作薄 
    self.wkbook=xlwt.Workbook() 
    #創(chuàng)建表:“埋點(diǎn)需求” 
    self.dataSheet=self.wkbook.add_sheet("shellt") 
    self.creatHead() 
     
  def creatHead(self): 
    firstLine=[] 
    #創(chuàng)建表頭 
    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如何實(shí)現(xiàn)excel讀寫數(shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI