如何使用 python 将文件通过管道传输到流中

how to pipe a file into stream using python

我正在尝试将我的 xls 文件输出重定向到一个流,但我遇到了错误。

下面是我使用的代码

#!/usr/bin/env python

import codecs

import datetime

import decimal

import csv

import sys

import xlrd

def main(self):
    # We need to do this dance here, because we aren't writing through agate.
    if six.PY2:
        stream = codecs.getwriter('utf-8')(self.output_file)
    else:
        stream = self.output_file

def dump_xls(data,active_worksheet):
                active_worksheet.append(data)
     file_name = "temp.xls"


     reader = csv.reader(self.input_file,delimiter=",")

     workbook  = openpyxl.Workbook()
     worksheet = workbook.active
     for i in reader:
         dump_xls(i,worksheet)
     workbook.save(file_name)

def launch_new_instance():
    utility = CSVXLS()
    utility.run()


if __name__ == '__main__':
    launch_new_instance

()

我已经有了一个输入流和所有的流信息。我如何将我的输出文件转换为流,以便数据在输出中可见。

我是 python 的新手,非常感谢任何帮助。

非常感谢!!!

我不知道我明白你在找什么stream,但是StringIO也是一个流,你可以这样写

import StringIO
...
...
s_io = StringIO.StringIO() # Declare stream
for i in reader:
    dump_xls(i,worksheet)
    s_io.write(i) # Write to stream.

最后,s_io将代表stream