python提取Excel中的特定列生成新的表格
#coding=utf-8
import xlrd,chardet,traceback,csv
#根据列名获取相应序号
def getColumnIndex(table,columnName):
columnIndex=None
for i in range(table.ncols):
if(table.cell_value(0,i)==columnName):
columnIndex=i
break
return columnIndex
#根据Excel中sheet名称读取数据
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生成现金流套表/房源清单-(截止1031).xlsx‘
sheetName=‘Sheet1‘
table=readExcelDataByName(fileName,sheetName)
list=[]
for i in range(0,8000):
try:
xm=table.cell_value(i,getColumnIndex(table,‘项目‘))
cplx=table.cell_value(i,getColumnIndex(table,‘产品类型‘))
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()
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16