zoukankan      html  css  js  c++  java
  • python处理excel

    import xlrd
    import xlwt
    path = input("请输入文件路径")
    listnum = ["0","1","2","3","4","5","6","7","8","9"]
    listSign = []
    workbook = xlrd.open_workbook(path)
    sheet = workbook.sheet_by_index(0)
    #sheet = workbook.sheet_by_name(sheetname) outfile
    = xlwt.Workbook(encoding='utf-8') sheetout = outfile.add_sheet('sheet1') for row in range(0,sheet.nrows): for i in range(0,len(sheet.row(row)[0].value)): if(((sheet.row(row)[0].value[i] in listnum or sheet.row(row)[0].value[i]=='.')and not(sheet.row(row)[0].value[i-1] in listnum or sheet.row(row)[0].value[i-1]=='.'))or(not(sheet.row(row)[0].value[i] in listnum or sheet.row(row)[0].value[i]=='.')and(sheet.row(row)[0].value[i-1] in listnum or sheet.row(row)[0].value[i-1]=='.'))): listSign.append(i) print(listSign) sheetout.write(row,0,sheet.row(row)[0].value[0:listSign[0]]) for i in range(0,len(listSign)-1): sheetout.write(row,i+1,sheet.row(row)[0].value[listSign[i]:listSign[i+1]]) sheetout.write(row,len(listSign),sheet.row(row)[0].value[listSign[len(listSign)-1]:]) listSign = [] outfile.save('outputfile1.xls')

    把三列拆开右边到左边。

    import xlrd
    import xlwt
    path = input("请输入文件路径")
    workbook = xlrd.open_workbook(path)
    sheet = workbook.sheet_by_index(0)
    ##for row in range(2,sheet.nrows):
    ##    print()
    ##    for col in range(sheet.ncols):
    ##        print("%7s"%sheet.row(row)[col].value,'	',end='')
    #        print(int(sheet.row(row)[col].value))
    #sheet.nrows
    #pathout = input("请输入输出文件路径")
    outfile = xlwt.Workbook(encoding='utf-8')
    sheetout = outfile.add_sheet('sheet1')
    sheetout.write(0,0,'房间号')
    sheetout.write(0,1,'电表名称')
    sheetout.write(0,2,'电表号')
    j=0
    for i in range(0,sheet.ncols,2):
        for row in range(2,sheet.nrows):
            if type(sheet.row(row)[i+1].value).__name__=='float':
                j=j+1
                sheetout.write(j,0,sheet.row(row)[i].value)
                num=sheet.row(row)[i+1].value+180613000000
                num=int(num)
                sheetout.write(j,1,num)
                sheetout.write(j,2,num)
    outfile.save('outputfile.xls')
  • 相关阅读:
    [问题2014A05] 复旦高等代数 I(14级)每周一题(第七教学周)
    oracle的相关信息
    进程和线程的区别
    阿里云人脸检测定位
    php与oracle11g经典分页
    Linux 远程复制
    redis的相关信息
    教你编译PHP7 (nginx+mysql+php7)
    nginx的相关信息
    php about session store db or cache
  • 原文地址:https://www.cnblogs.com/qijunzifeng/p/12176367.html
Copyright © 2011-2022 走看看