将流对象指向另一个流

我在外部位置有一个文件流对象,并希望将该流作为类似文件的对象传递给pygresql。那行得通,但是我想用替换对流进行一些更改。

import gzip
import pgdb

stream = gzip.open(external_location,'r')
myConnection = pgdb.connect( host='x.x.x.X',user='test',password='test',database='test' )
cursor = myConnection.cursor()
cursor.copy_from(stream,'test_table')

我不想在本地保存文件的临时副本。我想将流直接指向PostgreSQL,但是在旅途中的每次迭代中都要进行一些更改。

stream = gzip.open(external_location,'r')
manipulated_stream = (line.replace('a','b') for line in stream.readlines())
cursor.copy_from(manipulated_stream,'test_table')

但是作为一个类似文件的对象,我可以将其传递给copy_from命令。

wuwenqian1986 回答:将流对象指向另一个流

最后,我找到了this解决方案,该解决方案基本上是根据生成器表达式创建stringIOBase类。

本文链接:https://www.f2er.com/3122609.html

大家都在问