溫馨提示×

溫馨提示×

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

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

python提取Excel中的特定列生成新的表格

發(fā)布時(shí)間:2020-08-06 04:44:19 來源:網(wǎng)絡(luò) 閱讀:1328 作者:wx5d72071a58c07 欄目:編程語言
#coding=utf-8
import xlrd,chardet,traceback,csv

#根據(jù)列名獲取相應(yīng)序號
def getColumnIndex(table,columnName):
    columnIndex=None 
    for i in range(table.ncols):
        if(table.cell_value(0,i)==columnName):
            columnIndex=i
            break
    return columnIndex

#根據(jù)Excel中sheet名稱讀取數(shù)據(jù)
def readExcelDataByName(fileName,sheetName):
    table=None     
    try:
        data=xlrd.open_workbook(fileName)
        table=data.sheet_by_name(sheetName)
    except Exception:
        pass       
    return table

if __name__=='__main__':
    #example
    csv_file=open('房源清單.csv','w+',newline='',encoding='utf-8')
    writer=csv.writer(csv_file)

    fileName=r'/Users/Desktop/python/python生成現(xiàn)金流套表/房源清單-(截止1031).xlsx'
    sheetName='Sheet1'

    table=readExcelDataByName(fileName,sheetName)

    list=[]   
    for i in range(0,8000):
        try:
            xm=table.cell_value(i,getColumnIndex(table,'項(xiàng)目'))
            cplx=table.cell_value(i,getColumnIndex(table,'產(chǎn)品類型'))
            fymc=table.cell_value(i,getColumnIndex(table,'房源名稱'))

        except Exception:
            pass
        list.append([xm,cplx,fymc])

    for row in list:
        writer.writerow(row)

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

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

AI