【问题标题】:Pointing a stream object to another stream将流对象指向另一个流
【发布时间】:2020-03-07 09:19:09
【问题描述】:

我在外部位置有一个文件流对象,并希望将该流作为类文件对象传递给 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 命令。

【问题讨论】:

    标签: python python-3.x postgresql io stream


    【解决方案1】:

    最后我找到了this 解决方案,它基本上是从生成器表达式创建一个stringIOBase 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2011-06-22
      • 2013-04-28
      • 2018-09-17
      相关资源
      最近更新 更多