1

I am using python v2.7.3 and am trying to get a conversion to work but am having some issues.

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:

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

9
  • 1
    You say you're on 3.x, but you're using 2.x print syntax. Are you sure you know what version you're using? Commented Jun 2, 2014 at 20:41
  • possible duplicate of Best way to convert string to bytes in Python 3? Commented Jun 2, 2014 at 20:47
  • This code doesn't produce the output you say it does on Python 3. It produces a SyntaxError. Commented Jun 2, 2014 at 20:50
  • Hi, I checked and you are right. I am running version 2.7.3. My sincerest apologies for the confusion. Commented Jun 2, 2014 at 21:15
  • 1
    Please print out the repr() value of the string you want to convert. Commented Jun 2, 2014 at 21:35

1 Answer 1

1

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.

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).

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.

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.