Home > OS >  How do I remove the b'' after retrieving data from mysql to Python
How do I remove the b'' after retrieving data from mysql to Python

Time:01-26

  1. I get the data from MySQL server, and format it into a list of lists.
raw_data = read_query(connection, query)
csv_data = []
for row in raw_data:
    row = list(row)
    csv_data.append(row)
  1. Convert the data into Dataframe, and export as CSV
df = pd.DataFrame(csv_data)
df.to_csv('test.csv', index=False)

How can I get rid of the b'' in all columns where it is present? I tried to change encoding='utf-8', the b'' is still there. It seems the dataframe takes the b'' as a string instead of proper byte?

CodePudding user response:

Use str.decode:

df = pd.DataFrame({'A': [b'Hello', b'world']})

          A
0  b'Hello'
1  b'world'


df['A'] = df['A'].str.decode('utf-8')
print(df)

# Output
       A
0  Hello
1  world

CodePudding user response:

Decode the members of row.

raw_data = read_query(connection, query)
csv_data = []
for row in raw_data:
    row = list(row)
    row = [x.decode() for x in row]
    csv_data.append(row)
  •  Tags:  
  • Related