使用python将Oracle中的Blob数据转换为文本文件 [英] Blob data from Oracle to text file using python
本文介绍了使用python将Oracle中的Blob数据转换为文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试使用Python将oracle中的blob数据转换为文本文件.我在其他任何链接上都找不到答案.
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.
下面是我的代码:
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()
我收到以下错误
TypeError: utf_8_encode() argument 1 must be str, not cx_Oracle.LOB
我尝试了其他几种方法,但是问题是,即使我看到的其他方法也只能处理字符串,而不能处理blob.
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.
推荐答案
您必须使用cx_oracle.LOB.read()
方法来获取LOB
对象的内容:
You have to use the cx_oracle.LOB.read()
method to get the content of the LOB
object:
f.write(row[2].read())
这篇关于使用python将Oracle中的Blob数据转换为文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文