如何使用python将文件传输到stream中
我想redirect我的xls文件输出到一个stream,但我面临错误。
以下是我正在使用的代码
#!/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 ()
我已经有一个inputstream和所有的stream信息。 我怎样才能将我的输出文件转换为stream,使数据在输出中可见。
我是新的python,任何帮助非常感谢。
非常感谢 !!!
我不知道我明白你在找什么stream
,但是StringIO
也是一个stream,你可以这样写
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