import zipfile
z = zipfile.ZipFile("D:\\img.zip", 'r' )
for f in z.namelist():
    a_f=f.split('/')[1] 
    if a_f.startswith('S'): 
       print ('S')  
    elif a_f.startswith('H'):    
       print('H')
    else:
       continue   
import zipfile
zin= zipfile.ZipFile('E:\\img.zip','w'):
for f in zin.namelist():
    a_f=f.split('/')[1] 
    print (a_f)
zin.close()
         with zin.open(a_f,'w') as f:
            corpus_chars = f.read().decode('utf-8')
         if a_f.startswith('S'):
            print ('S')  
         elif a_f.startswith('H'):  
            print('H')   
# 最终有效的脚本

import zipfile
zin= zipfile.ZipFile('E:\\img.zip','r')
print (zin)
for f in zin.namelist():
    aa=zin.open('img/H001.jpg','r')
    print (aa.read())
zin.close() 

dtcloud里的解析zip包:
在这里插入图片描述

import base64
import zipfile
from io import BytesIO


def btn_confirm(self):
    """
       base64_data = base64.b64encode(f.read())
    """
    zip_data = base64.decodebytes(self.package_file)
    fp = BytesIO()
    fp.write(zip_data)
    zip_f = zipfile.ZipFile(fp, "r")
    talent_obj = self.env['talent.information'].sudo()
    no_find = []
    for zf in zip_f.namelist():
        aa = zip_f.open(zf, 'r')
        a_f = zf.split('/')[1]
        if a_f.startswith('S'):
            identification = a_f.split('.')[0].lstrip('S')
            info_id = talent_obj.search([('identification_id', '=', identification)], limit=1)
            if not info_id:
                no_find.append(a_f)
            if info_id:
                info_id.write({'image_1920': base64.b64encode(aa.read())})
        elif a_f.startswith('H'):
            passport_number = a_f.split('.')[0].lstrip('H')
            info_id = talent_obj.search([('passport_number', '=', passport_number)], limit=1)
            if not info_id:
                no_find.append(a_f)
            if info_id:
                info_id.write({'image_1920': base64.b64encode(aa.read())})
        else:
            continue
    zip_f.close()
    message = '图片全部导入成功'
    if no_find:
        message = '下列图片【%s】在系统中未找到对应的人员' % ','.join(no_find)
    notification = {
        'type': 'ir.actions.client',
        'tag': 'display_notification',
        'params': {
            'title': u'图片导入',
            'message': message,
            'sticky': False,
            'type': 'success',
            'next': {
                'type': 'ir.actions.act_window_close'
            },
        },
    }
    return notification
Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐