python oracle blob_使用python将Oracle中的Blob数据导入文本文件

该博客讲述了使用Python从Oracle数据库中提取BLOB类型的数据,并将其写入文本文件的过程。作者遇到了将BLOB转换为字符串的问题,最终通过调用cx_Oracle.LOB的read()方法成功解决了问题。
摘要由CSDN通过智能技术生成

I have been trying to get the blob data from oracle into a text file using Python. I couldn't find the answer on any of the other links.

Below is my code :

sql_string = """select

event_id

,blob_length

,blob field

from table"""

cur.execute(sql_string)

path = "P:/Folders/"

for row in cur:

filename = path + "notes_" + str(row[0]) + "_" + str(row[1]) + ".txt"

f = codecs.open(filename, encoding='utf-8', mode='wb+')

f.write(row[2])

f.close()

I get the below error

TypeError: utf_8_encode() argument 1 must be str, not cx_Oracle.LOB

I have tried a few other ways but the problem is that even other approaches that I've seen only handle strings and not blobs.

解决方案

You have to use the cx_oracle.LOB.read() method to get the content of the LOB object:

f.write(row[2].read())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值