简体   繁体   English

如何将字符串转换为字节

[英]How do you convert string to bytes

I am using python v2.7.3 and am trying to get a conversion to work but am having some issues. 我正在使用python v2.7.3,正在尝试进行转换,但出现了一些问题。

This is code that works the way I would like it to: 这是我想要的代码:

testString = "\x00\x13\xA2\x00\x40\xAA\x15\x47"
print 'Test String:',testString 

This produces the following result 这将产生以下结果

TestString: ¢@ªG

Now I load the same string as above along with some other data: 现在,我加载与上述相同的字符串以及一些其他数据:

\x00\x13\xA2\x00\x40\xAA\x15\x47123456

into a SQLite3 database and then pull it from the database as such: 进入SQLite3数据库,然后将其从数据库中拉出,如下所示:

cur.execute('select datafield from databasetable')
rows = cur.fetchall()
if len(rows) == 0:
    print 'Sorry Found Nothing!'
else:
    print row[0][:32]

This however produces the following result: 但是,这将产生以下结果:

\x00\x13\xA2\x00\x40\xAA\x15\x47

I can not figure out how to convert the database stored string to the bytes string, if that is what it is, as the first snippet of code does. 我无法弄清楚如何将数据库存储的字符串转换为字节字符串(如果是的话),如第一段代码一样。 I actually need it to load into a variable in that format so I can pass it to a function for further processing. 我实际上需要它以这种格式加载到变量中,以便可以将其传递给函数进行进一步处理。

The following I have tried: 我尝试了以下方法:

print "My Addy:",bytes(row[0][:32])
print '{0}'.format(row[0][:32]) 
...

They all produce the same results... 它们都产生相同的结果...

Please

First, Can anyone tell me what format the first results are in? 首先,谁能告诉我第一个结果的格式是什么? I think its bytes format but am not sure. 我认为它的字节格式,但不确定。

Second, How can I convert the database stored text into 第二,如何将数据库存储的文本转换为

Any help and I would be eternally grateful. 任何帮助,我将永远感激不已。

Thanks in advance, 提前致谢,

Ed 埃德

The problem is that you're not storing the value in the database properly. 问题是您没有将值正确存储在数据库中。 You want to store a sequence of bytes, but you're storing an escaped version of those bytes instead. 您想存储一个字节序列,但是要存储这些字节的转义版本。

When entering string literals into a programming language, you can use escape codes in your source code to access non-printing characters. 在将字符串文字输入到编程语言中时,可以在源代码中使用转义码来访问非打印字符。 That's what you've done in your first example: 这就是您在第一个示例中所做的:

testString = "\x00\x13\xA2\x00\x40\xAA\x15\x47"
print 'Test String:',testString 

But this is processing done by the Python interpreter as it's reading through your program and executing it. 但这是Python解释器在读取并执行程序时完成的处理。

Change the database column to a binary blob instead of a string, then go back to the code you're using to store the bytes in SQLite3, and have it store the actual bytes ( 'ABC' , 3 bytes) instead of an escaped string ( '\\x41\\x42\\x43' , 12 characters). 将数据库列更改为二进制blob而不是字符串,然后返回到用于在SQLite3中存储字节的代码,并使其存储实际字节( 'ABC' ,3个字节)而不是转义字符串( '\\x41\\x42\\x43' ,12个字符)。

If you truly need to store the escaped string in SQLite3 and convert it at run-time, you might be able to use ast.literal_eval() to evaluate it as a string literal. 如果确实需要将转义的字符串存储在SQLite3中并在运行时进行转换,则可以使用ast.literal_eval()将其评估为字符串文字。

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

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