Is there any safe way of mixing binary with text data in a (pseudo)csv file?
One naive and partial solution would be:
If you need everything in a single file, just use one of the methods to encode binary as printable ASCII, and add that results to the CSV vfieds (letting the CSV module add and escape quotes as needed).
One such method is
base64 - but even on Python's base64 codec, there are more efficient codecs like base85 (on newer Pythons, version 3.4 and above, I guess).
So, an example in Python 2.7 would be:
import csv, base64 import random data = b''.join(chr(random.randrange(0,256)) for i in range(50)) writer = csv.writer(open("testfile.csv", "wt")) writer.writerow(["some text", base64.b64encode(data)])
Of course, you have to do the proper base64 decoding on reading the file as well - but it is certainly better than trying to create an ad-hoc escaping method.