If you get a binary input of tabular structured data, you can convert it to CSV easily by using
str(byte)[2:-1] slicing to get rid of the
b'...' byte wrapper and apply
string.replace('\t', ',') on the result to replace tabular characters with commas.
For example, say you have the following bytes format in your Python code (e.g., as returned by an API or as read from a binary file):
b'...' prefix structure shows that it is of type
byte = b'Name\tProfession\tAge\tIncome\nAlice\tProgrammer\t26\t88000\nBob\tEngineer\t21\t33000\nCarl\tStudent\t19\t-12000' print(type(byte)) # <class 'bytes'>
So, how to convert the
bytes object to a CSV?
You can convert a bytes object to a string in three steps:
- Convert the
byteobject to a string using
[2:-1]to get rid of the
- Convert the string to a CSV formatted string by using
string.replace('\\t', ',').replace('\\n', '\n'), for instance.
- Write the CSV to a file by using the
Here’s the minimal code example you can copy&paste:
byte = b'Name\tProfession\tAge\tIncome\nAlice\tProgrammer\t26\t88000\nBob\tEngineer\t21\t33000\nCarl\tStudent\t19\t-12000' # 1. convert byte to string csv = str(byte)[2:-1] # 2. convert string to csv csv = csv.replace('\\t', ',').replace('\\n', '\n') # 3. write csv to file print(csv, file=open('my_file.csv', 'w'))
The resulting CSV file looks like this:
Note, we use the double escape
\\n notation in order to get rid of the special meaning of the tab and newline because in the converted string obtained from the byte representation, the tab and newline characters are now represented with two characters
't' and newline characters with two characters
For example, if you print the original converted string (from byte) characters, you see that issue:
csv = str(byte)[2:-1] for i,c in enumerate(csv): print(i,c) ''' Output: 0 N 1 a 2 m 3 e 4 \ 5 t 6 P 7 r 8 o 9 f 10 e 11 s 12 s 13 i 14 o 15 n '''
In other words, we want to replace the sequence of those two characters with the single comma and single newline character.
You can convert it to any other format as specified in this related tutorial:
🌍 Related Resource: Ultimate Guide to Convert a CSV to another format
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.