前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pandas 大文件操作

pandas 大文件操作

作者头像
Kevinello
发布2022-08-19 11:07:20
1.5K0
发布2022-08-19 11:07:20
举报

常规的读取大文件的步骤

代码语言:javascript
复制
import pandas as pd

f = open('./data/ows-raw.txt',encoding='utf-8')
reader = pd.read_table(f,  sep=',',  iterator=True, error_bad_lines=False) #跳过报错行
loop = True
chunkSize = 100000
chunks = []


while loop:
    try:
        chunk = reader.get_chunk(chunkSize)
        chunks.append(chunk)
    except StopIteration:
        loop = False
        print("Iteration is stopped.")


df = pd.concat(chunks, ignore_index=True)

STORY

这几天有一个需求是读取.dta文件并转为.csv,google了一下发现pandas也是支持dta格式的

于是直接开写,20行搞定

然而事情并没有那么简单…

read_stata方法就直接抛出ValueError了:

1
1

又Google了一下,github issues上没有解决了的,stackoverflow里倒是有提议,但貌似不是抛出这个error

解决

无奈还是自己去读源码了,发现StataReader的get_chunk方法貌似在不给出chunksize时不能默认读取全部,无奈只能采用了下面的方法二分chunksize直到读取完毕:

代码语言:javascript
复制
import pandas as pd
import os
import re


target_path = './data/excel/{}.csv'


def dta_to_excel(origin_path):
    CHUNKSIZE = 2000
    reader = pd.read_stata(origin_path, iterator=True)
    file_name = re.sub(r'\.dta', '', origin_path.split('/')[-1])
    print('{} translate start'.format(file_name))

    chunks = []
    while CHUNKSIZE > 0:
        try:
            print('Will get {} lines'.format(CHUNKSIZE))
            chunk = reader.get_chunk(CHUNKSIZE)
            chunks.append(chunk)
        except ValueError:
            print('CHUNKSIZE too large')
            CHUNKSIZE //= 2
    
    df = pd.concat(chunks, ignore_index=True)
    df.to_csv(target_path.format(file_name))
    print('{} translated done'.format(file_name))


if __name__ == '__main__':
    origin_dir = './data/origin'
    #  os.listdir:列出目标路径下的所有文件(文件夹)
    for path in os.listdir(origin_dir):
        dta_to_excel(os.path.join(origin_dir, path))

总算是能正常输出了…

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-11-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 常规的读取大文件的步骤
  • STORY
  • 解决
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档