python合并相同表头表格,反解XML流

1、合并相同表头表格

代码如下

#python pandas合并表格插件
#coding:gbk
import os,time
import pandas as pd


class Excel_concat(object):
    def __init__(self,FILES_PATH,NEW_FILE_PATH,FILES):
        self.files_path = FILES_PATH
        self.new_file_path = NEW_FILE_PATH
        self.file_list = FILES #文件列表
        self.one_sheet = [] #要合并的子sheet1
        self.two_sheet = [] #要合并的子sheet2
        self.rows = ['登记证明编号','关联的初始登记编号'] #要处理的表头

    def rows_utils(self,sheet):
    #列数据处理,将int数字形式转化为文本格式(字符串)
        for i in self.rows :
            sheet[i] = [str(i) for i in sheet[i]]
        return sheet

    def read_excel(self,file):
    #读取excel返回pd对象
        file_sheet0 = self.rows_utils(pd.read_excel(os.path.join(self.files_path,file), sheet_name=0,encoding='gbk'))
        file_sheet1 = self.rows_utils(pd.read_excel(os.path.join(self.files_path,file), sheet_name=1,encoding='gbk'))
        return file_sheet0,file_sheet1


    def write_excel(self):
        writer = pd.ExcelWriter(self.new_file_path)
        pd.concat(self.one_sheet).to_excel(writer, 'sheet1', index=False)
        pd.concat(self.two_sheet).to_excel(writer, 'sheet2', index=False)
        writer.save()


    def run(self):
        for i in self.file_list:
            try:
                print(i)
                sh0, sh1 = self.read_excel(i)
                self.one_sheet.append(sh0)
                self.two_sheet.append(sh1)
            except Exception as e:
                print('文件内容错误',i)
        self.write_excel()


def subutils(data):
#正则处理空白字符串(空格,换行符等)
    import re
    data = re.sub('[\s]', '', data)
    return data

if __name__ == '__main__':
    start = time.time()
    FILES_PATH = r'D:\xxxx\xxxxx\xxxx\xxxx\xxx'
    NEW_FILE_PATH = r'D:\xxxx\xxxx\xxxxx\newxls1.xlsx'
    FILES = os.listdir(FILES_PATH)
    obj = Excel_concat(FILES_PATH,NEW_FILE_PATH,FILES[0:2])
    print(FILES)
    print(len(FILES))
    # obj.run()
    # pf1,pf2 = read_excel(FILES[0])
    # print(pf1)
    # print(pf2)
    end = time.time()
    print('Running Time is: ', str(end - start) + 's')

 

2、反解XML流

代码如下

#反解xml流,经过base64加密加zip打包
import base64
from io import BytesIO
import zipfile

content = b'<?xml version="1.0" encoding="gbk"?>' \
          b'<BODY><xmlResultByte>' \
          b'UEsDBBQACAAIAG9WhE8AAAAAAAAAAAAAAAAbAAAAMjAxOTEyMDQxMDUxMzA5ODQxMDc1OTUueG1ss7GvyM1RKEstKs7Mz7NVMtQzUFJIzUvOT8nMS7dVCg1x07VQsrfjsklLTU1JSkzOtuPitMlLzE21e7pz89N1nS8nb3uxsMdGHywElErOz0vJLAEaZWejGO3s4hjiGP1szq5n81qeTuh9vnzDo4YpSs9mTH6xZe6TPTOe9kx7um7Rs47tT/bNfrprytOpbS9nbwOiJzs6lWJj7Wz0EYYBTS7JBNpgZGBoqWtopGtgomBoYGVqaGVsYKMPlgGpyC9JzCnKLy+2AwnCOUCZlMSSRBCtD2HY6MN9AwBQSwcIYdLQ59oAAAD/AAAAUEsBAhQAFAAIAAgAb1aET2HS0OfaAAAA/wAAABsAAAAAAAAAAAAAAAAAAAAAADIwMTkxMjA0MTA1MTMwOTg0MTA3NTk1LnhtbFBLBQYAAAAAAQABAEkAAAAjAQAAAAA=' \
          b'</xmlResultByte></BODY>'

zip1=base64.b64decode(content)

fio = BytesIO(zip1)
myzip = zipfile.ZipFile(file=fio)
print(myzip.namelist())
print(myzip.read(myzip.namelist()[0]).decode())
posted @ 2020-01-06 17:47  xiaomogugu  阅读(1156)  评论(0编辑  收藏  举报