GithubHelp home page GithubHelp logo

bz2_rl's Introduction

bz2_rl

PyPI version

Helper class for processing large bzip2 compressed text files efficiently without high memory usage.

I originally wrote this class to help me run through a large SQL dump which was over 90 MB uncompressed. This class basically just wraps the streaming bzip2 decompressor from the standard library in an iterator, making it very easy to run through the lines of a compressed file without actually loading the file into memory.

This is specifically built for text files.

Tested with Python 3.5, but should work for all Python 3 versions.

Installation

Install using pip, e.g.

$ pip install bz2_rl

Or add it as dependency to your own project's setup.py.

Usage

Usage is very simple: make an instance with at least the path to the file as argument, then iterate over it. For example:

from bz2_rl import BZ2FileIter
sql_lines = BZ2FileIter('exports/database.sql')
for line in sql_lines:
    # ...

The following arguments are supported:

  • encoding - specifies encoding of the compressed text (default: utf-8)
  • read_size - amount of bytes to read per iteration (default: 512)
  • linebreak - turns off linebreak auto-detection and forces a specific type (default: None)

Linebreaks are auto-detected and converted to the system default.

The file is closed automatically after the last line. If you don't need to use the file anymore, simply delete the BZ2FileIter instance to close the file.

Development

I'm always glad to accept pull requests or to look at issues or questions.

Tests

Run ./setup.py test to run the unit tests.

License

MIT licensed.

bz2_rl's People

Contributors

msikma avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.