Python Convert Bytes to CSV

4/5 - (1 vote)

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.

Problem Formulation

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):


The b'...' prefix structure shows that it is of type bytes:

byte = b'Name\tProfession\tAge\tIncome\nAlice\tProgrammer\t26\t88000\nBob\tEngineer\t21\t33000\nCarl\tStudent\t19\t-12000'
# <class 'bytes'>

Quick Solution

So, how to convert the bytes object to a CSV?

You can convert a bytes object to a string in three steps:

  1. Convert the byte object to a string using str() and slicing [2:-1] to get rid of the "b'...'" enclosing notation.
  2. Convert the string to a CSV formatted string by using string.replace('\\t', ',').replace('\\n', '\n'), for instance.
  3. Write the CSV to a file by using the print() function with file=open(..., 'w') argument.

Minimal Example

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 \\t and \\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 '\' and 't' and newline characters with two characters '\' and 'n'.

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):

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