I am trying to follow the solution from question How to write a csv file in binary mode? (or somewhat related Python: reading from binary zipfile into csv) by playing with a test code, but don't know how to exactly handle the actual file.
Basically, I want the original b'\x01'
byte value to be preserved as bytes type in the end when it comes back out, but in my code it comes out as string.
Also on Linux (Raspberry Pi OS) Python 3.7 the test code below throws AttributeError: 'file' object has no attribute 'readable'
on line 8, whereas on Windows 10 / Python 3.9 the test code runs without any error.
How can I handle the file properly in this context ?
Test code:
import io
import csv
test_byte = b'\x01'
print ("original item value is %s of type %s" % (test_byte, type(test_byte)))
csv_data_dict = [{"preset": "item", "value": test_byte}]
with io.TextIOWrapper(open("test.csv", 'wb'), encoding = 'utf-8', newline = '') as csvfile:
fieldnames = ["preset", "value"]
writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
writer.writeheader()
for i in range(len(csv_data_dict)):
writer.writerow(csv_data_dict[i])
with io.TextIOWrapper(open("test.csv", 'rb'), encoding = 'utf-8', newline = '') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row["preset"] == "item":
print ("item value returned as %s of type %s" % (row["value"], type(row["value"]))) # comes back a string
And its output (on Windows):
original item value is b'\x01' of type <class 'bytes'>
item value returned as b'\x01' of type <class 'str'>