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

python 打印文件内容

f = open(filename,mode)#filename,文件路径、名称,如果有\,注意转义或者开头加r,mode读取的模式,r读取,w写入,,read = f.read()#获取内容并储存在变量里f.close()#关闭文件,节省内存

以模块的 import 为例: 1.首先写一个自定义模块 Hello.py def greet(): print("Hello World~~~") 2.然后写一个测试文件 test.py 去调用这个模块 import HelloHello.greet() 3.运行效果:

f = open("test.txt","r")line = f.readlines()f.close()for item in line: print(item)

$ 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...

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

用a模式(append)打开文件, f = open('test.txt','a') f.write('c1') f.close()

#vim info.xml 信息 auto_userone Jordy #vim infoxmlparser.py #!/usr/bin/python#coding=utf-8#Python Version Python 2.6.5from xml.sax.handler import ContentHandlerfrom xml.sax import parseclass HeadlineHandler(ContentHandler): in_...

输出的时候将print换成写入文件方式 fout = open("a.txt", "w+", encoding="utf-8")fout.write("this string will be output in txt.")

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

# -*- coding: utf-8 -*-""":created on: 2015年7月13日:copyright: Nokia Solutions and Networks:author: Chuanqing Qin:contact: chuanqing.qin@nokia.com"""import reimport osif __name__ == '__main__': counter = 0 with open('test.txt'...

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