zoukankan      html  css  js  c++  java
  • Python_Example_Excel读取后存储显示_实现程序

    2018-09-17

    Author: 楚格

    IDE: Pycharm2018.02   Python 3.7   

    KeyWord :  Excel display

    Explain:   读取Excel,显示和存储读取的数据

     思路:

    1. 确定打开Excel的文件,包括路径以及名称

    2.对Excel进行操作,分片是基础操作

    3.处理的目的:使分片的每行数据(列表数据)映射成字典(名称是键,每行内容是值),每行的数据聚合成列表,即列表中元素是字典。

    4.显示,打印出每行的数据集

    1------------------------------------------------------------------------------------------------------------------

      1 __all__ = ["Display_Testing_Config"]
      2 
      3 # coding=utf-8
      4 import sys
      5 import os
      6 import pprint
      7 from collections import OrderedDict  # 字典元素有序排列
      8 
      9 # 读取 Excel 模块
     10 import xlrd
     11 import xlwt
     12 
     13 '''
     14 # ------------------------------------------
     15 # 导入:外文件夹中 .py 模块
     16 # 先导入路径,后引用模块
     17 # ------------------------------------------
     18 '''
     19 # 1 D:Auto_Testing_PycharmProtocol_ConfigsConfigs
     20 sys.path.insert(0,"./Configs")
     21 from Testing_Config_List import *
     22 
     23 
     24 '''
     25 # ------------------------------------------
     26 # 导入:本文件夹中 .py 模块
     27 # 直接引用模块名称
     28 # ------------------------------------------
     29 '''
     30 # project module
     31 from Testing_CRC import *
     32 
     33 #-----------------------------------------------------------------------------
     34 
     35 '''
     36 # ============================================================================
     37 # Function:  测试函数  函数
     38 # Explain : 
     39 #         :
     40 #  file   :  本文件连接正确
     41 # ============================================================================
     42 '''
     43 def Display_Testing_Config():
     44     print("Function: Testing_Config    Ready ")
     45     print("File    : Protocol_Configs  ok 
    ")
     46 
     47 
     48 
     49 '''
     50 # ============================================================================
     51 # Function:   文件路径以及文件名  函数
     52 # Explain : 
     53 #         :
     54 #  file   : # file= r'D:Auto_Testing_PycharmProtocol_ConfigsConfigs attribute_protocol.xlsx' #固定路径
     55 #         # use protocol of handle  file  path   
     56 #           local_var_file_path = Function_Select_File()   # 可变路径
     57 #
     58 # ============================================================================
     59 '''
     60 # 单独列出 已被查看
     61 local_var_file_path = Function_Select_File()
     62 def Function_Excel_Table_Byname(file=local_var_file_path,colnameindex=0, by_name=u'Sheet1'):  # 修改自己路径
     63     print('Function_Excel_Table_Byname')
     64     #---------------------------------------------------------------------------------------
     65     local_list_line      = []
     66     local_dictionary_app = {}  # 列表变成字典
     67 
     68     #--------------------------------------------------------------------------------------
     69     # local_var_temp_data  = Function_Open_Excel(file)      # 打开电子文档
     70     local_var_temp_data = xlrd.open_workbook(file)          # 打开电子文档  目的:响应删除函数
     71     # # 这里可以拓展,使用一个Excel 利用不同表格,可以减少外部表格的数量,与上述打开表格方式 是不同的
     72     local_var_temp_table = local_var_temp_data.sheet_by_name(by_name)   # 获得表格 Excel中不同表格
     73     # # Contains the data for one worksheet
     74     local_var_total_rows = local_var_temp_table.nrows                   # 属性 拿到总共行数
     75     # print('total_rows:  < %d >' % local_var_total_rows)               # display
     76     # # Returns a slice of the values of the cells in the given row
     77     local_var_column_names = local_var_temp_table.row_values(colnameindex, 0, 5)  # 某一列数据 ['', '', '', ''] 默认从0行开始
     78     # print('column num: < %s >   names:  < % s >'% (len(local_var_column_names),local_var_column_names))
     79 
     80 
     81     for temp_num in range(1, local_var_total_rows):  # 也就是从Excel第二行开始,第一行表头不算
     82         local_var_row = local_var_temp_table.row_values(temp_num)  # 返回单元格中切片
     83         # print('rows_num: < %s >  line_num: < %s >  each_element' % (temp_num ,len(local_var_row)),local_var_row )  # 每row 就是列表
     84 
     85         # 每行分片成列表, 列表转变成字典, 字典转变成列表
     86         local_dictionary_app = dict(zip(local_var_column_names,local_var_row))  #列表变字典
     87         # print('local_dictionary_app: ',local_dictionary_app)
     88         local_list_line.append(local_dictionary_app)        # 字典转变成列表
     89 
     90     # 内部的字典变成了元素
     91     # print('local_list_line: ',local_list_line)  #打印列表元素
     92     # 遍历列表中的元素  显示成表格模式
     93     for row in local_list_line:
     94         print('行号编码:< %s >  %s'%(local_list_line.index(row),row))
     95     #     pprint.pprint(row)  # 规则打印数据
     96 
     97     # 返回列表 并且列表的元素是字典
     98     return local_list_line
     99 
    100 # ---------------------------------------------------------------------------
    101 '''
    102 # ============================================================================
    103 # Function:     类
    104 # Explain :  
    105 #         :
    106 # ============================================================================
    107 '''
    108 class Class_Module_Use:
    109     def __init__(self):
    110         print("-----")
    111         print("+++++")
    112 
    113 # ============================================================================
    114 
    115 
    116 '''
    117 # ============================================================================
    118 #   测试专用
    119 # ============================================================================
    120 '''
    121 if __name__ == "__main__":
    122     print('测试开始')
    123     Function_Excel_Table_Byname()  # 获取到列表 列表中每个元素是字典
    124     module = Class_Module_Use()
    125     print('测试完成')

    --

    RUN Result

    --

     1 Protocols that can be executed :  
     2  0 attribute_protocol.xlsx
     3  1 read_test - 副本.xlsx
     4  2 read_test.xlsx
     5  3 SG80KTL.xlsx
     6  5 z.xlsx
     7 List number : [0, 1, 2, 3, 5]
     8 Please your select file number : 
     9  ->>> 
    10 Your select protocol : < attribute_protocol.xlsx > 
    11 temp_var_path_string:  D:Auto_Testing_PycharmProtocol_ConfigsConfigsattribute_protocol.xlsx 
    12 测试开始
    13 Function_Excel_Table_Byname
    14 行号编码:< 0 >  {'0参数': '整机属性协议编号', '1地址': 2500.0, '2长度': 2.0, '3类型': 1.0, '4支持': 1.0}
    15 行号编码:< 1 >  {'0参数': '整机属性协议版本', '1地址': 2502.0, '2长度': 2.0, '3类型': 1.0, '4支持': 1.0}
    16 行号编码:< 2 >  {'0参数': '整机属性协议长度', '1地址': 2504.0, '2长度': 4.0, '3类型': 1.0, '4支持': 1.0}
    17 行号编码:< 3 >  {'0参数': '整机属性协议编码', '1地址': 2508.0, '2长度': 2.0, '3类型': 2.0, '4支持': 1.0}
    18 行号编码:< 4 >  {'0参数': '应用协议保留长度', '1地址': 2510.0, '2长度': 10.0, '3类型': 1.0, '4支持': 0.0}
    19 -----
    20 +++++
    21 测试完成
    22 
    23 Process finished with exit code 0
  • 相关阅读:
    C# 操作配置文件
    C# Excel操作类
    没有找到 mspdb100.dll 的解决办法
    工厂方法模式
    .Net互操作2
    The certificate used to sign “AppName” has either expired or has been revoked. An updated certificate is required to sign and install the application解决
    手机抓包xcode自带命令行工具配合wireshark实现
    expecting SSH2_MSG_KEX_ECDH_REPLY ssh_dispatch_run_fatal问题解决
    使用ssh-keygen设置ssh无密码登录
    远程复制文件到服务器
  • 原文地址:https://www.cnblogs.com/caochucheng/p/9662481.html
Copyright © 2011-2022 走看看