数据处理之Excel—2

版权声明:本文为博主原创文章,转载请注明作者和出处。https://blog.csdn.net/xq920831/article/details/84391226

记录自己的数据处理经验,加快数据筛选。

这篇文章主要完成了用python读取ini文件内容并把感兴趣的部分写入Excel表格存储。

代码如下:

# -*- coding:utf-8 -*-
# Author: Agent Xu

import os
import glob
import configparser
import xlsxwriter

ini_dir = 'E:/gongjingaiNO3/'
# xls_dir = 'E:/第三批数据整理.xlsx'
xls_dir = 'E:/sheet1.xlsx'
#读取图片
a=0

def read_ini(path):
    global a
    XM = []
    NL = []
    YDJ = []
    NZ = []
    FC = []
    XBX = []
    cate = [path+x for x in os.listdir(path) if os.path.isdir(path+x)]
    for idx,folder in enumerate(cate):
        for im in glob.glob(folder+'/*.ini'):
            config = configparser.ConfigParser()
            config.read(im)
            a = config.get("PatientInfo","NianLing")
            b = config.get("PatientInfo","YinDaoJingTuXiang")
            c = config.get("PatientInfo","NiZhen")
            d = config.get("PatientInfo","XingMing")
            e = config.get("PatientInfo","JinYiBuChuLiYiJian")
            f = config.get("PatientInfo","XiBaoXue")
            NL.append(a)
            YDJ.append(b)
            NZ.append(c)
            XM.append(d)
            FC.append(e)
            XBX.append(f)
    return XM,NL,YDJ,NZ,FC,XBX

# print(read_ini(ini_dir))
xm,nl,ydj,nz,fc,xbx = read_ini(ini_dir)

# 建立文件
workbook = xlsxwriter.Workbook(xls_dir)

worksheet = workbook.add_worksheet()
# 按列插入数据
worksheet.write_column('A2',xm)
worksheet.write_column('B2',nl)
worksheet.write_column('F2',xbx)
worksheet.write_column('G2',ydj)
worksheet.write_column('H2',nz)
worksheet.write_column('I2',fc)

workbook.close()

还有一些ini文件的处理技巧,参考:https://www.cnblogs.com/liuyl-2017/p/7833986.html

以及,一些Excel表格的操作,参考:https://www.cnblogs.com/276815076/p/8028127.html

                                                                 https://www.cnblogs.com/shaosks/p/6098282.html

                                                                 https://www.jb51.net/article/137061.htm

猜你喜欢

转载自blog.csdn.net/xq920831/article/details/84391226