1

I have column in a csv that has this value as string:

b'\x00\x00\x00\xc0\x99MC@\x00\x00\x00\xc0\x99MC@\x00\x00\x00\xc0\x99MC@\x00\x00\x00\xc0\x99MC@\x00\x00\x00\xc0\x99MC@\x00\x00\x00\xc0\x99MC@'

How can I convert this string in a bytes object in python?

I tried bytes(string_var, 'utf-8') but I got:

b"b'\\x00\\x00\\x00\\xc0\\x99MC@\\x00\\x00\\x00\\xc0\\x99MC@\\x00\\x00\\x00\\xc0\\x99MC@\\x00\\x00\\x00\\xc0\\x99MC@\\x00\\x00\\x00\\xc0\\x99MC@\\x00\\x00\\x00\\xc0\\x99MC@'"

I am reading the csv file with pandas

thanks in advance

[edit]

answers to comments:

1- my expected result is: b'\x00\x00\x00\xc0\x99MC@\x00\x00\x00\xc0\x99MC@\x00\x00\x00\xc0\x99MC@\x00\x00\x00\xc0\x99MC@\x00\x00\x00\xc0\x99MC@\x00\x00\x00\xc0\x99MC@' (with only one 'b' and object type as bytes and not string)

2-I got this csv from: sql with byte array column => pandas dataframe => csv file

ps: before create csv file from pandas I use this command:

blob['Raw'] = blob['Raw'].apply(lambda x: x.tobytes())

otherwise, the csv file will store '<memory at 0x7f697cdc9d08>' string

3
  • 3
    Use ast.literal_eval(). Commented Feb 10, 2021 at 19:50
  • what result do you expect? As for me this is already bytes string - but Python shows it in different way than you probably expect. If it can't display value as char then it display its code - ie. \x00 Commented Feb 10, 2021 at 21:38
  • are you sure you have real csv ? How did you create this file? Or where did you get it? maybe it is not csv but some numpy.array or image. Commented Feb 10, 2021 at 21:40

1 Answer 1

1

@Barmar is lifesaver

I used ast.literal_eval and worked:

from ast import literal_eval

blob_csv['Raw'] = blob_csv['Raw'].apply(lambda x: literal_eval(str(x)))

Thanks to Barmar and How to use ast.literal_eval in a pandas dataframe and handle exceptions

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.