pandas操作excel

根據.conf配置文件,拿到要讀出的列名,一行一個字典,存放於列表裏

pandas讀取read_excel和to_excel各參數詳解:https://blog.csdn.net/u010801439/article/details/80052677

pandas讀取excel常用操作: https://www.cnblogs.com/liulinghua90/p/9935642.html

# -*- coding: utf-8 -*-
# @Time    : 2019/3/27 13:35
# @Author  : guigle
# @File    : resd_excel.py
# @Software: PyCharm

import configparser
import pandas


def load_excel():
    data01 = list()
    # data02 = list()
    df_01 = pandas.read_excel("resident_export_212737441126158336.xlsx")
    # 對值爲nan的改爲 ''
    df_02 = df_01.where(df_01.notnull(), "")
    # df_02 = df_01.fillna('')

    # df_02 = pandas.read_excel("resident_export_212737441126158336.xlsx", sheet_name=1)
    # pandas讀取excel時產生Unnamed:列  需要進行刪除 
    # old_titles = df_01.columns  # 讀取表格標題行
    # for title in old_titles:
    #     if 'Unnamed' in title.split(":"):
    #         del df_01[title]
    temp_path = 'resident_temp.conf'
    cf = configparser.ConfigParser()
    cf.read(temp_path, encoding='utf-8')
    # 根據配置文件拿到要篩選的表格標題
    colum_li = cf.options('residentList')

    for i in df_02.index.values:
        # row_data = df_02.ix[i, colum_li].to_dict()
        # 不指定讀取哪些列,默認讀出所有列
        row_data = df_02.loc[i, colum_li].to_dict()
        data01.append(row_data)
    # for j in df_02.index.value:
    #     row_data = df_01.loc[j,].to_dict()
    #     data02.append(row_data)
    print(data01)
    # print(data02)


load_excel()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章