wlbk.net
当前位置:首页 >> python 打印到文件 >>

python 打印到文件

list=[[1,2,3],[4,5,6],[7,8,9]] f=open('demo.txt','w') for i in list: k=' '.join([str(j) for j in i]) f.write(k+"\n") f.close()

首先导入sys模块 import sys 然后在打算把输出数据写入文件的代码之前加上以下代码 output=sys.stdout outputfile=open(filename,'w') sys.stdout=outputfile 上面的filename表示输出文件 程序结束或恢复成正常输出时加上以下代码 outputfile.cl...

name =['a1','a2','a3']seq=['seq11111','seqs22222','seq33333']f = open("F:/1.txt", "w+")f.write("name\tseq\n")for i in range(0, len(name)):f.write(name[i] + "\t" + seq[i] + "\n")f.close()

f = open("lianxi2.txt",'wb')print >>f, 'useno:'k=0while (k>f, useno k = k+1print >>f, '-------------------------------------------------------'print >>f, 'usepsw:'h=0while (h>f, usepsw h = h+1f.close()

# writefout=open('test','w');fout.write(u'\u66fe'.encode('utf-8'));# readfin = open('test','r');print fin.read().decode('utf-8');

a="内容"f=open('new.txt', 'a+')# 将函数a里面的内容输出到文件f.write(a)f.close

fout = open('output.txt', 'w') # 打开输出文件...print >>fout, what_you_want_to_print # 所有的print语句都如此修改...fout.close() # 最后关闭文件这是python2.7中基本的写文件操作。

$ python3Python 3.2.3 (default, Feb 27 2014, 21:33:50) [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import codecs>>> with codecs.open("test.txt", 'r', 'gbk') as handle:... for l...

因为print是输出到sys.out里面, 所以主要思路就是替换标准的输出, 写了一个例子供你参考: Python代码 # coding: utf-8 class TextArea(object): def __init__(self): self.buffer = [] def write(self, *args, **kwargs): self.buffer.append(ar...

import osdn=os.getcwd() #获取当前目录L=os.listdir(dn)with open('result.txt','w') as wf: wf.write('\n'.join(L))

网站首页 | 网站地图
All rights reserved Powered by www.wlbk.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com