开发者

Convert Java byte array to Python byte array

I know that if I figure this one out or if somebody shows me, it'll be a forehead slapper. Before posting any questions, I try for at least three hours and quite a bit of searching. There are several 开发者_如何学编程hints that are close, but nothing I have adopted/tried seems to work.

I am taking a byte[] from Java and passing that via JSON (with Gson) to a python JSON using Flask. This byte[] is stored in a python object as an integer list when received, but now I need to send it to MySQLdb and store it as a blob. The data contents is binary file data.

How do I convert a python integer list [1,2,-3,-143....] to something that I can store in MySQL? I have tried bytearray() and array.array(), but those choke when I access the list directly from the object and try and convert to a string to store thorugh MySQLdb.

Any links or hints are greatly appreciated.


You can join it into a bytestring (just a string under python 2.x). The simplest, if not most efficient, way would be to just mod the data, then convert to chars and join. Something like:

data = [1,2,-3,-143, ...]
binData = ''.join(map(lambda x: chr(x % 256), data))

binData = ''.join(map(lambda x: chr(x % 256), attach.attcoll))
    sql_stmt = """INSERT INTO attachments (attno,filename,fileextension,projNo,procNo,wpattachment) \
    VALUES ('%s','%s','%s','%s','%s','%s') ON DUPLICATE KEY UPDATE filename='%s',fileextension='%s'""" % (attach.attno,\
    attach.filename,attach.fileextension,attach.projNo,attach.procNo,binData,attach.filename,attach.fileextension)

    try:
        cursor.execute(sql_stmt)
        conn.commit()
        cursor.close()
        conn.close()
        return 'SUCCESS'
    except MySQLdb.Error:
        cursor.close()
        conn.close()
        print "My SQL cursor execute error."
        return 'FAILURE'


data = [1,2,-3,-143, ...]
binData = bytearray([x % 256 for x in data])


I found ''.join(map(lambda x: chr(x % 256), data)) to be painfully slow (~4 minutes) for my data on python 2.7.9, where a small change to str(bytearray(map(lambda x: chr(x % 256), data))) only took about 10 seconds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜