zoukankan      html  css  js  c++  java
  • python操作execl学习笔记(一)

    本节只记录关于execl的读操作:

     execl 内容及格式

    python3

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import xlrd
    import xlwt
    import datetime
    def read_file():
        ###打开execl文件
        file_name = xlrd.open_workbook("C:\Users\admin\Desktop\关键词统计.xlsx")
        #### 获取所有sheet名称,得到一个列表 ['10-28', '10-29', '10-30', '10-31', '11-1']
        print (file_name.sheet_names())   ###打印所有sheet名称
    
        # 根据sheet索引或者名称获取sheet内容
        sheet_1031 = file_name.sheet_by_index(3)               ###根据索引获取sheet名称及内容
        sheet_1031bak = file_name.sheet_by_name('10-31')       #### 根据sheet名称获取内容
        # sheet的名称,行数,列数
        print (sheet_1031.name,sheet_1031.nrows,sheet_1031.ncols)
        a = sheet_1031.nrows
        b = sheet_1031.ncols
        
       
      ### 类型说明 ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
      ###默认从0行0列开始算起
      print (sheet_1031.cell(0,1).ctype)      ###打印获得的execl值的类型(第一行第二列)
        # 获取整行和整列的值
        rows = sheet_1031.row_values(1)     ###获取第一行的值---列表
        cols = sheet_1031.col_values(2)     ###获取第一列的值---列表
    
        print (rows)
        for i in range(a):
            for j in range(b):
                if i == 0:
                    break
                elif (sheet_1031.cell(i,j).ctype) == 3:
                    year, month, day, hour, minute, second = xlrd.xldate_as_tuple(sheet_1031.cell(i,j).value,
                                                                                  file_name.datemode)
                    py_date = datetime.datetime(year, month, day, hour, minute, second)
                    print ("时间:",py_date)
                elif (sheet_1031.cell(i,j).ctype) == 2 :
                    c = int(sheet_1031.cell(i,j).value)
                    if j == 0:
                        print ("整时:",c)
                    else:
                        print ("用户id:",c)
    
                elif (sheet_1031.cell(i, j).ctype) == 1:
                    e = sheet_1031.cell(i, j).value
                    if j == 3:
                        print ("关键词:",e)
                    else:
                        print ("ip:",e)
    
    
    
    
        ###列的开头从0开始,行的开头也从0刚开始
        print (rows,cols)
        d = int(sheet_1031.cell(1,0).value)
        print (d)               ####打印第二行第一列
        # print (sheet_1031.cell_value(1,3))          ####打印第二行第四列
        # print (sheet_1031.row(2)[4].value)          ####打印第2行第4列
        ####打印第四行第一列的值的类型
        # print (sheet_1031.cell(4,0).ctype)          ### ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
    
        ####时间拆分得到一个元组
        # data_value = xlrd.xldate_as_tuple(sheet_1031.cell_value(4,1),file_name.datemode)
        # print (data_value)
        ###默认情况下得到的时间为一个时间戳,下面代码将时间戳转化为所需要的execl时间
        year, month, day, hour, minute, second = xlrd.xldate_as_tuple(sheet_1031.cell(4, 1).value,file_name.datemode)
        py_date = datetime.datetime(year, month, day, hour, minute, second)
        print (datetime.datetime(year, month, day, hour, minute, second))
    
    
    read_file()
    
    
  • 相关阅读:
    百家号开发文档测试
    python使用selenium模拟登录网易
    python使用selenium模拟操作Chrome浏览器
    java 解析网易邮箱里面的附件地址,获取下载地址
    python连接hive
    linux重定向标准输入输出,标准错误
    linux 查看网络流量命令
    kafka源码阅读环境搭建
    tomcat启动index页面显示不出来
    git学习笔记
  • 原文地址:https://www.cnblogs.com/FRESHMANS/p/6041719.html
Copyright © 2011-2022 走看看