There is a list of bytes objects (each one is 4 bytes) that is returned as output of one code and I want to save it into a .csv file using CSV module and read it back later in another script. Here is the code that I have learnt from python's official documentation:
import struct
import csv
k = 0x100000
rng = range(0, k)
x1 = [b''] * k
x = 0xffffffff
for i in rng:
x1[i] = struct.pack("<L", x)
x -= 1
print(x1[0]) # b'\xff\xff\xff\xff'
List = x1
with open("test.csv", 'w', newline='') as rF:
wr = csv.writer(rF, dialect='excel')
for i in List:
wr.writerow(i)
When looking inside the created test.csv using notepad, instead of a column of byte strings I see 4 columns of 8-bit integers. Few first lines of test.csv are:
255,255,255,255
254,255,255,255
253,255,255,255
252,255,255,255
251,255,255,255
250,255,255,255
.
.
.
What am I doing wrong that this is happening? Is there a way to get a csv file with one column of byte strings? for example:
b'\xff\xff\xff\xff'
b'\xfe\xff\xff\xff'
b'\xfd\xff\xff\xff'
.
.
.
Actually I do not care how are my bytes stored in a csv. I just care to have them back into a list of bytes using csv.reader in another script and want the loading process be the quickest possible.
CSVcan't save binary data. Think about using rawbinaryfile instead ofcsv.