简体   繁体   中英

Printing/Parsing csv blob file from mysql

Querying blob files in database, is it possible to read as text from blob files rather than array

Here my simple code:

sql= "select blob_file as blob_file from Blob_Table limit 1"                                          
cursor.execute(sql)
result = cursor.fetchall()

for i in result :
    print i["file_backup"]

My output is something like these, an array.array type:

array('b', [67, 97, 114, 114, 105, 101, 114, 32, 73, 68, 44, 83, 116, 97, 103, 101, 32, 82, 111, 119, 44, 83, 116, 97, 103, 101, 32, 67, 111, 108, 117, 109, 110, 44, 68, 97, 116, 101, 44, 84, 105, 109, 101, 44, 65, 65, 66, 32, 50, 76, 32, 68, 101, 112, 116, 104, 44, 65, 65, 66, 32, 67, 97, 118, 105, 116, 121, 32, 82, 97, 44, 65, 65, 66, 32, 67, 97, 118, 105, 116, 121, 32, 68, 101, 112, 116, 104, 44, 65, 65, 66, 32, 65, 66, 83, 32, 81, 117, 97, 108, 105, 116, 121, 32, 111, 102, 32, 70, 105, 116, 44, 67, 111, 110, 102, 105, 103, 32, 110, 97, 109, 101, 40, 110, 97, 109, 101, 32, 111, 110, 108, 121, 41, 44, 65, 65, 66, 32, 65, 66, 83, 32, 80, 105, 120, 32, 67, 111, 117, 110, 116, 44, 65, 65, 66, 32, 50, 110, 100, 32, 76, 118, 108, 32, 80, 105, 120, 32, 67, 111, 117, 110, 116, 44, 65, 65, 66, 32, 67, 97, 118, 105, 116, 121, 32, 80, 105, 120, 32, 67, 111, 117, 110, 116, 44, 65, 65, 66, 32, 67, 97, 118, 105, 116, 121, 32, 82, 113, 44, 65, 65, 66, 32, 65, 66, 83, 32, 81, 117, 97, 100, 32, 67, 114, 111, 119, 110, 44, 84, 101, 109, 112, 108, 97, 116, 101, 32, 70, 105, 116, 32, 81, 117, 97, 108, 105, 116, 121, 44, 84, 101, 109, 112, 108, 97, 116, 101, 32, 82, 111, 116, 97, 116, 105, 111, 110, 44, 84, 101, 109, 112, 108, 97, 116, 101, 32, 88, 32, 84, 114, 97, 110, 108, 97, 116, 105, 111, 110, 44, 84, 101, 4, 54, 13, 10])

Is possible to convert these because my blob file is pure text from csv..

First, the type you have there appears to be a byte array. For the purpose of testing, I run the following to get myself the same byte array as you:

raw_results = bytearray([67, 97, 114, 114, 105, 101, 114, 32, 73, 68, 44, 83, 116, 97, 103, 101, 32, 82, 111, 119, 44, 83, 116, 97, 103, 101, 32, 67, 111, 108, 117, 109, 110, 44, 68, 97, 116, 101, 44, 84, 105, 109, 101, 44, 65, 65, 66, 32, 50, 76, 32, 68, 101, 112, 116, 104, 44, 65, 65, 66, 32, 67, 97, 118, 105, 116, 121, 32, 82, 97, 44, 65, 65, 66, 32, 67, 97, 118, 105, 116, 121, 32, 68, 101, 112, 116, 104, 44, 65, 65, 66, 32, 65, 66, 83, 32, 81, 117, 97, 108, 105, 116, 121, 32, 111, 102, 32, 70, 105, 116, 44, 67, 111, 110, 102, 105, 103, 32, 110, 97, 109, 101, 40, 110, 97, 109, 101, 32, 111, 110, 108, 121, 41, 44, 65, 65, 66, 32, 65, 66, 83, 32, 80, 105, 120, 32, 67, 111, 117, 110, 116, 44, 65, 65, 66, 32, 50, 110, 100, 32, 76, 118, 108, 32, 80, 105, 120, 32, 67, 111, 117, 110, 116, 44, 65, 65, 66, 32, 67, 97, 118, 105, 116, 121, 32, 80, 105, 120, 32, 67, 111, 117, 110, 116, 44, 65, 65, 66, 32, 67, 97, 118, 105, 116, 121, 32, 82, 113, 44, 65, 65, 66, 32, 65, 66, 83, 32, 81, 117, 97, 100, 32, 67, 114, 111, 119, 110, 44, 84, 101, 109, 112, 108, 97, 116, 101, 32, 70, 105, 116, 32, 81, 117, 97, 108, 105, 116, 121, 44, 84, 101, 109, 112, 108, 97, 116, 101, 32, 82, 111, 116, 97, 116, 105, 111, 110, 44, 84, 101, 109, 112, 108, 97, 116, 101, 32, 88, 32, 84, 114, 97, 110, 108, 97, 116, 105, 111, 110, 44, 84, 101, 4, 54, 13, 10])

Now I want to get this into an easier to work with format, that is a string: results = raw_results.decode('utf-8')

This should do it.

Going forward you will probably want to use the csv module to read your csv string so that you don't have to deal with issues like quoted commas. However, the csv reader typically reads from a file, but it works with anything that iterates over lines. So you probably want to do something like this to actually work with the data. See Python csv string to array for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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