python 解析zip包 zip包包含xls,下载zip包的内容,直接读取为pandas DataFrame数据框

zip包包含xls,下载zip包的内容,直接读取为pandas DataFrame数据框

分别是ZipFile和ZipInfo两个类的基本操作:
python对zip流进行压缩和解压
一个案例看机器学习建模基本过程

zip包结构
xxx.zip
  --abc (文件夹)
  	--- 1.xls
  	--- 2.xls
# -*- coding: utf-8 -*-

"""
@Softwhare:win10  Python 3.6.3 |Anaconda, Inc.| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)] on win32
@IDE--Env : PyCharm--
@Time    : 2020/9/24 20:20
@Author  : chentan--bob
@connect : chentan@zto.com
@File    : python下载zip文件.py
@Version :  1.0.0 
@Desc    :
@LastTime: 
"""
## 从本地读取
# import zipfile, os
# import pandas as pd
# # filename_zip= r'D:\home\baseuser\spider\spider\CN\cngg_spider\scrapyapp\other-spiders\tmpzip.zip'
# filename_zip= r'D:\home\baseuser\spider\spider\CN\cngg_spider\scrapyapp\other-spiders\2582028_20200924_144945_01.zip'
# z = zipfile.ZipFile(filename_zip, 'r')
# for filename in z.namelist()[:]:
#     # sertch .txt
#     print(filename)
#     f = z.open('2582028/1.xls')
#     # f = z.read(filename)
#     # print(f)
#     # df = pd.read_csv(f,sep='|')
#     df = pd.read_excel(f)
#     print(df.head())

## 从网络上下载
import zipfile
from io import BytesIO

import pandas as pd
import requests


def parsr_downloadTask_zip3(url_zip=''):
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "max-age=0",
        "Connection": "keep-alive",
        "Cookie": "HWWAFSESID=6116e8be308c92f15e; HWWAFSESTIME=1600950140695",
        "Host": "mirrors.huaweicloud.com",
        "Referer": "https://mirrors.huaweicloud.com/python/",
        "Sec-Fetch-Dest": "document",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Site": "same-origin",
        "Sec-Fetch-User": "?1",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
    }

    data_dic_new = "121212"
    # startDate_ = data_dic_new.get('startDate', '') / 1000
    # startDate = datetime.fromtimestamp(startDate_).strftime('%Y%m%d_%H%M%S')
    # time.sleep(0.5)
    print(url_zip)
    res = requests.get(url_zip, headers=headers)
    # res = requests.get(url_zip)
    # print(res.text)
    # print(f"开始下载zip3,{res.url}")
    if res.status_code == 200:
        print("下载zip字节流并转为DataFrame")
        fio = BytesIO(res.content)
        z = zipfile.ZipFile(file=fio)
        print(z)
        for filename in z.namelist()[:]:
            print(filename)
            f = z.open(filename)
            df = pd.read_excel(f)
            print(df.head())

        with open(f'./python.zip', 'wb+')as fp:
            fp.write(res.content)
            # fp.flush()
        print(f"下载成功zip3{data_dic_new}...")
    else:
        print(f"下载失败{data_dic_new}\n{res.text}...")

    a = '1'
    return a


url = 'https:xxxx.zipXXX'
parsr_downloadTask_zip3(url_zip=url)


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值