对python-3-print重定向输出的几种方法总结


Posted in Python onMay 11, 2018

方法1:

import sys 
 
f=open('test.txt','a+') 
a='123' 
b='456' 
print >> f,a,b 
f.close()

方法2:

import sys 
 
f=open('a.txt','w') 
old=sys.stdout #将当前系统输出储存到临时变量 
sys.stdout=f #输出重定向到文件 
print 'Hello World!' #测试一个打印输出 
sys.stdout=old  #还原系统输出 
f.close() 
print open('a.txt','r') # 错误的方法,仅用于查看输出,了解python 
print open('a.txt','r').read()
import sys 
year=1 
years=15 
bj=10000 
rate=0.05 
f=open('total.txt','w+') 
while year < years: 
   bj=bj*(1+rate) 
   print >> f,"第%d年,本息合计%0.2f" % (year,bj) 
   year+=1

方法3:

自行编写一个类,这个类只要有write函数,以模拟file类型就可以将系统输出重定向到其上。

class FakeOut: 
 def __init__(self): 
  self.str='' 
  self.n=0 
 def write(self,s): 
  self.str+="Out:[%s] %s\n"%(self.n,s) 
  self.n+=1 
 def show(self): #显示函数,非必须 
  print self.str 
 def clear(self): #清空函数,非必须 
  self.str='' 
  self.n=0 
f=FakeOut() 
import sys 
old=sys.stdout 
sys.stdout=f 
print 'Hello weird.' 
print 'Hello weird too.' 
sys.stdout=old 
f.show() 
# 输出: 
# Out:[0] Hello weird. 
# Out:[1] 
 
# Out:[2] Hello weird too. 
# Out:[3]

以上这篇对python-3-print重定向输出的几种方法总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用 Selenium 实现网页截图实例
Jul 18 Python
Centos7 Python3下安装scrapy的详细步骤
Mar 15 Python
利用Python如何制作好玩的GIF动图详解
Jul 11 Python
3个用于数据科学的顶级Python库
Sep 29 Python
Python解决线性代数问题之矩阵的初等变换方法
Dec 12 Python
Django csrf 两种方法设置form的实例
Feb 03 Python
python读写csv文件方法详细总结
Jul 05 Python
Pytorch 保存模型生成图片方式
Jan 10 Python
Django实现将views.py中的数据传递到前端html页面,并展示
Mar 16 Python
Python创建临时文件和文件夹
Aug 05 Python
详解python变量与数据类型
Aug 25 Python
分享几种python 变量合并方法
Mar 20 Python
利用Python如何实现数据驱动的接口自动化测试
May 11 #Python
Python数据结构之图的应用示例
May 11 #Python
python 重定向获取真实url的方法
May 11 #Python
基于python 爬虫爬到含空格的url的处理方法
May 11 #Python
解决Python 爬虫URL中存在中文或特殊符号无法请求的问题
May 11 #Python
解决Python网页爬虫之中文乱码问题
May 11 #Python
解决python爬虫中有中文的url问题
May 11 #Python
You might like
将word转化为swf 如同百度文库般阅读实现思路及代码
2013/08/09 PHP
ThinkPHP模版引擎之变量输出详解
2014/12/05 PHP
PHP设置进度条的方法
2015/07/08 PHP
php中define用法实例
2015/07/30 PHP
php生成4位数字验证码的实现代码
2015/11/23 PHP
php自定义函数实现汉字转换utf8编码的方法
2016/09/29 PHP
php多线程并发实现方法
2016/09/30 PHP
thinkPHP5框架分页样式类完整示例
2018/09/01 PHP
jquery $.ajax入门应用一
2008/11/19 Javascript
Javascript对象Clone实例分析
2015/06/09 Javascript
全面解析Bootstrap中scrollspy(滚动监听)的使用方法
2016/06/06 Javascript
jQuery中的AjaxSubmit使用讲解
2016/09/25 Javascript
Vue自定义指令拖拽功能示例
2017/02/17 Javascript
Bootstrap下拉菜单Dropdowns的实现代码
2017/03/17 Javascript
Vue.js框架路由使用方法实例详解
2017/08/25 Javascript
nodejs实现超简单生成二维码的方法
2018/03/17 NodeJs
vue-cli2.0转3.0之项目搭建的详细步骤
2018/12/11 Javascript
微信小程序视图控件与bindtap之间的问题的解决
2019/04/08 Javascript
jQuery控制input只能输入数字和两位小数的方法
2019/05/16 jQuery
Python 处理数据的实例详解
2017/08/10 Python
Python引用传值概念与用法实例小结
2017/10/07 Python
Python内置random模块生成随机数的方法
2019/05/31 Python
通过python实现弹窗广告拦截过程详解
2019/07/10 Python
Python SSL证书验证问题解决方案
2020/01/13 Python
Python基于stuck实现scoket文件传输
2020/04/02 Python
python3发送request请求及查看返回结果实例
2020/04/30 Python
HTML5实现直播间评论滚动效果的代码
2020/05/27 HTML / CSS
草莓网美国官网:Strawberrynet USA
2016/12/11 全球购物
俄罗斯连接商品和买家的在线平台:goods.ru
2020/11/30 全球购物
2015年学校安全管理工作总结
2015/05/11 职场文书
2015年乡镇安全生产工作总结
2015/05/19 职场文书
实施意见格式范本
2015/06/05 职场文书
大学生十八大感想
2015/08/11 职场文书
Mac环境Nginx配置和访问本地静态资源的实现
2021/03/31 Servers
html+css实现环绕倒影加载特效
2021/07/07 HTML / CSS
MySQL索引失效场景及解决方案
2022/07/23 MySQL