Python 代码段

python zip文件操作


# 将zip文件写入到数据库中
def read_zip():
    file_path = "./data/000001.SZ.zip"
    with ZipFile(file_path, 'r') as zf:
        flen = len(zf.namelist())
        index = 0
        for name in zf.namelist():
            index += 1
            if ZipInfo(name).is_dir():
                continue
            df = pd.read_table(io.BytesIO(zf.read(name)), sep=",")
            code, f_full_name = os.path.split(name)
            code, code_prefix = code[0:6], code[-2:].lower() + code[0:6]
            fname, fsuffix = os.path.splitext(f_full_name)
            # {'code': '603993', 'price': 4.54, 'volume': 308380834, 'amount': 1374993729.0, 'time': 20200803150000110}
            result = []
            for findex, row in df.iterrows():
                obj = {
    
    
                    'code': code,
                    'price': row['last'],
                    'volume': row['volume'],
                    'amount': row['amt'],
                    'time': fname.replace("-", "") + row['time'].replace(":", '') + '000'
                }
                result.append(obj)
            if result:
                to_5_db(result, stock_codes=[code_prefix])
            print("进度:%d/%d, %s" % (index, flen, f_full_name))

猜你喜欢

转载自blog.csdn.net/afgasdg/article/details/107775311