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

python 打印到文件

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()

利用write函数写入文件,例如 a = ["a","b","c"] file = open('test.txt','w') for i in a: file.write(i) file.close() newfile = open("test.txt") newfile.read()

#-*- coding:utf-8 -*-f = open('123.txt', 'r') #文件为123.txtsourceInLines = f.readlines() #按行读出文件内容f.close()new = [] #定义一个空列表,用来存储结果for line in sourceInLines: temp1 = line.strip('\n') #去掉每行最后的换行符...

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()

# encoding:utf-8import osprint os.path.abspath("test")使用os.path.abspath方法可以输出文件夹的绝对路径。 参数里面要写相对路径,比如脚本和文件夹在同一路径,就直接写名字即可。如果文件夹在上一个目录,就写成"../test"。 其实从方法名...

$ 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换成写入文件方式 fout = open("a.txt", "w+", encoding="utf-8")fout.write("this string will be output in txt.")

#注意,这里的代码用单空格缩进import re#写上你的文件夹路径yourdir=""keywordA = "keywordA"keywordB = "keywordA(\d+)"files = [os.path.join(yourdir,f) for f in os.listdir(yourdir)]with open("out.txt","w") as fo: for f in files: fna...

f1=open("a.txt","r") content=f1.readlines() f1.close() f1=open("a.txt","w") f2=open("b.txt","a") F = "" for line in content: if(line.strip() == "#test end"): F = "a" f.write(line) continue elif(line.strip() == "#test start"): F...

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()

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