簡體   English   中英

如何寫一個python function使用LZ4壓縮一個CSV文件

[英]How to write a python function to compress a CSV file using LZ4

這里是新手...我需要使用 LZ4 讀取和壓縮 CSV 文件,但我遇到了預期錯誤,compress() function 以字節為單位讀取,而 CSV 文件不兼容。 有沒有辦法使用LZ4壓縮整個文件,還是需要將CSV文件轉換成bit格式再壓縮? 如果是這樣,我將如何處理這個問題?

import lz4.frame
import csv

file=open("raw_data_files/raw_1.csv")
type(file)
input_data=csv.reader(file)
compressed=lz4.frame.compress(input_data)

錯誤顯示

Traceback (most recent call last):
  File "compression.py", line 10, in <module>
    compressed=lz4.frame.compress(input_data)
TypeError: a bytes-like object is required, not '_csv.reader'

你可以這樣做:-

import lz4.frame

with open('raw_data_files/raw_1.csv', 'rb') as infile:
    with open('raw_data_files/raw_1.lz4', 'wb') as outfile:
        outfile.write(lz4.frame.compress(infile.read()))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM