Package: compressed-spreadsheets
Compressed SpreadsheetsCompressed Spreadsheets is a simple Python library for reading & writing togzipcompressed CSV files using a similar API as the builtincsv.DictReaderandcsv.DictWriter.PrioritiesSimplicitySpeedErgonomicsCompatibility with the API ofDictReaderandDictWriter(though not
the file format)CaveatsThis code assumes that each row has to correct number of elements, in order to avoid
imposing checks on each row.The goal of the implementation is to be reasonably fast with a simple implementation.
The CSVs it generates won't be compatible with any other library, because of the (simple, easy)
way special characters are escaped.If we were to useStringIOto create a buffer that a realDictWriterinstance
would write to, and then shuffle this into the compressed file, then we'd have
compatiblity without sacrificing simplicity; however, speed was more important
than compatiblity for my purposes, so I opted for this implementation.The library does not behave well on sheets with 0 columns.InstallationFrom GitHubSimply download the project and placecompressed_spreadsheets.pyinto your
project directory; it has no external requirements.From PyPIpip install compressed-spreadsheetsExamplesThese examples enumerate common use cases. See the docstrings for full documentation.Writing to a spreadsheetsheet = CompressedDictWriter.open("my_sheet.csv.gz", ("Column A", "Column B"))
sheet.writeheader()
sheet.writerow({"Column A": "Value 1", "Column B": "Value 2"})
sheet.writerows((
    {"Column A": "foo", "Column B": "bar"},
    {"Column A": "baz", "Column B": "snafu"}
))
sheet.close()Reading from a spreadsheetCallingCompressedDictReader.open(filename)returns an object we can iterate over
to retrieve our rows.sheet = CompressedDictReader.open("my_sheet.csv.gz")
# If the optional fieldnames argument is omitted, it is assumed the first line is a header row
next(sheet) # {"Column A": "Value 1", "Column B": "Value 2"}
for row in sheet:
    process(row)Specifying types for fieldsThefieldtypesargument allows you to automatically convert values into their proper types.write_sheet = CompressedDictWriter.open("my_numbers_sheet.csv.gz", fieldnames=("Column A", "Column B"))
write_sheet.writeheader()
write_sheet.writerow({"Column A": 10, "Column B": 5.1})
write_sheet.close()

read_sheet = CompressedDictReader.open("my_numbers_sheet.csv.gz", fieldtypes={"Column A": int, "Column B": float})
next(read_sheet) # {"Column A": 10, "Column B": 5.1})Context managersBothCompressedDictReaderandCompressedDictWritercan be used as context
managers. This will ensure the file is closed properly.with CompressedDictWriter.open("my_sheet.csv.gz") as sheet:
    for row in data:
        sheet.writerow(row)ContributingI'm open to contributions, and especially open to bug reports. Please open an
issue for any bugs, and please include unit tests & docstrings for any pull requests.Usepip -r development.txtto install the testing dependencies. Run tests withpytest. If you've made a very significant change or you'd like to hear for computer
fan, you can usepytest --hypothesis-profile hammerto generate 1000 testcases
for each test.LicenseCompressed Spreadsheets is distributed under the MIT license. See
LICENSE.txt for the full terms of the license.