简体   繁体   English

将字符串转换为 python 中的字节

[英]convert string to bytes in python

I have column in a csv that has this value as string:我在 csv 中有一个列,该列的值为字符串:

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?如何将此字符串转换为 python 中的字节 object?

I tried bytes(string_var, 'utf-8') but I got:我尝试bytes(string_var, 'utf-8')但我得到了:

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我正在用 pandas 读取 csv 文件

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) 1- 我的预期结果是: 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@'(只有一个 'b' 和 object 类型为字节而非字符串)

2-I got this csv from: sql with byte array column => pandas dataframe => csv file 2-我从 sql 得到这个 csv 字节数组列 => pandas dataframe => csv 文件

ps: before create csv file from pandas I use this command: ps:在从 pandas 创建 csv 文件之前,我使用这个命令:

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

otherwise, the csv file will store '<memory at 0x7f697cdc9d08>' string否则,csv 文件将存储 '<memory at 0x7f697cdc9d08>' 字符串

I have column in a csv that has this value as string:我在 csv 中有列,该列具有此值作为字符串:

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?如何将此字符串转换为 python 中的字节 object?

I tried bytes(string_var, 'utf-8') but I got:我试过bytes(string_var, 'utf-8')但我得到了:

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我正在阅读带有 pandas 的 csv 文件

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) 1-我的预期结果是: 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@' (只有一个 'b' 和 object 类型为字节而不是字符串)

2-I got this csv from: sql with byte array column => pandas dataframe => csv file 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: ps:在从 pandas 创建 csv 文件之前,我使用以下命令:

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

otherwise, the csv file will store '<memory at 0x7f697cdc9d08>' string否则,csv 文件将存储 '<memory at 0x7f697cdc9d08>' 字符串

@Barmar is lifesaver @Barmar 是救星

I usedast.literal_eval and worked:我使用ast.literal_eval并工作:

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感谢 Barmar 和How to use ast.literal_eval in a pandas dataframe and handle exceptions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM