import base64
string = 'Hello' # str
# encode string to bytes
str_binary = string.encode('utf-8') # bytes, b'Hello'
# encode binary data to printable ASCII characters
encoded_data = base64.b64encode(str_binary) # bytes, b'SGVsbG8='
# decode printable ASCII characters to binary data
decoded_data = base64.b64decode(encoded_data) # bytes, b'Hello'
# decode bytes to string
string = decoded_data.decode('utf-8') # str
num = 3.14
string = str(num) # convert float to str
str_binary = string.encode('utf-8') # encode string to bytes, b'3.14'
encoded_data = base64.b64encode(str_binary) # encode binary data to ASCII characters, b'My4xNA=='
decoded_data = base64.b64decode(encoded_data) # decode ASCII characters to binary data, b'3.14'
string = decoded_data.decode('utf-8') # convert bytes to string
num = float(string) # convert
import pandas as pd
data = {
'apples': [3, 2, 0, 1],
'oranges': [0, 3, 7, 2],
'category': ['A', 'B', 'A', 'C']
}
df = pd.DataFrame(data)
string = df.to_string() # convert dataframe to string
str_binary = string.encode('utf-8') # encode string to bytes
encoded_data = base64.b64encode(str_binary) # encode binary data to ASCII characters
decoded_data = base64.b64decode(encoded_data) # decode ASCII characters to binary data
string = decoded_data.decode('utf-8') # convert bytes to string
import io
df = pd.read_csv(io.StringIO(string), sep="\s+") # read pandas datafrmae from string
df
apples | oranges | category | |
---|---|---|---|
0 | 3 | 0 | A |
1 | 2 | 3 | B |
2 | 0 | 7 | A |
3 | 1 | 2 | C |