查询数据库中的blob文件,是否可以从blob文件而不是数组中读取文本
这是我的简单代码:
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"]
我的输出是这样的,array.array类型:
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])
可以转换这些因为我的blob文件是来自csv的纯文本。
答案 0 :(得分:0)
首先,你在那里的类型似乎是一个字节数组。出于测试目的,我运行以下命令以获得与您相同的字节数组:
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])
现在我想让这个更容易使用格式,这是一个字符串:
results = raw_results.decode('utf-8')
这应该这样做。
展望未来,您可能希望使用csv
模块来阅读您的csv字符串,这样您就不必处理引号等问题。但是,csv阅读器通常从文件中读取,但它适用于遍历行的任何内容。所以你可能想要做这样的事情来实际使用数据。有关示例,请参阅Python csv string to array。