I am trying to write data (contained in a dict) to a compressed (gzip) CSV file. As far as I understand the gzip.GzipFile method should accept a writing operation as a normal file object. Such as:
import gzip import csv with gzip.GzipFile(filename="test.csv.gz", mode="a") as gziphdlr: writer = csv.DictWriter(gziphdlr, fieldnames=['time','value']) writer.writeheader() writer.writerow({'time': 0.1, "value": 100}) writer.writerow({'time': 0.2, "value": 200}) However, I get the error:
... File "/usr/lib/python3.10/csv.py", line 154, in writerow return self.writer.writerow(self._dict_to_list(rowdict)) File "/usr/lib/python3.10/gzip.py", line 285, in write data = memoryview(data) TypeError: memoryview: a bytes-like object is required, not 'str' Any suggestions where I may be wrong?
Many thanks!
gzipas your variable name, you're redefining thegzipmodule.csvmodule outputs) you needmode='at'.