最近在进行多模态视频语义理解,环境配置anaconda3,python版本3.7.4,win10 系统,运行完一段代码后,发现print内置函数总是出错,出错内容如下:

D:\Anaconda3\lib\codecs.py in write(self, object)376         """377         data, consumed = self.encode(object, self.errors)
--> 378         self.stream.write(data)379 380     def writelines(self, list):D:\Anaconda3\Lib\site-packages\ipykernel\iostream.py in write(self, string)512         if not isinstance(string, str):513             raise TypeError(
--> 514                 f"write() argument must be str, not {type(string)}"515             )516 TypeError: write() argument must be str, not <class 'bytes'>

查看print函数的源代码,未找到,不过个人认为调用的顺序如下:

print()->stream.write()

print内置函数的help 文档(即help(print))如下:
Help on built-in function print in module builtins:
print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.

从上述文档中可以看出,print默认输出至sys.stdout,即标准输出,不过也可以输出至文件。而python 3.7.13的print内置函数官方文档如下:

print(*objectssep=' 'end='\n'file=sys.stdoutflush=False)

Print objects to the text stream file, separated by sep and followed by endsependfile and flush, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.

Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

Changed in version 3.3: Added the flush keyword argument.

发现出错的语句中有下面一句:

sys.stdout = codecs.getwriter("utf-8")(sys.stdout),

上述语句为强制类型转换,把sys.stdout的属性给修改了,修改前,print(sys.stdout)为:

ipykernel.iostream.OutStream object at 0x000001B5440914C8,

使用下面语句,进行临时输出(因为实际修改后则无法输出)

stdout_temp = codecs.getwriter("utf-8")(sys.stdout)
print(stdout_temp)

临时输出修改后的对象为:

encodings.utf_8.StreamWriter object at 0x000001B5451C6F08,

从上面可以看出两者不是同一个类,更不是同一个对象,导致write的参数出错,最终出现上面的错误。将上述语句sys.stdout = codecs.getwriter("utf-8")(sys.stdout),注释起来,然后重启jupyter内核,发现一切正常,所以系统变量不可轻易修改。

参考资料:

1Built-in Functions — Python 3.7.13 documentationhttps://docs.python.org/3.7/library/functions.html#print

python print 函数出错相关推荐

  1. Python print 函数- Python零基础入门教程

    目录 一.Python print 函数简介 二.Python print 函数语法 三.Python print 函数使用 1.objects 参数 2.sep 参数 3.end 参数 4.flus ...

  2. Python·.·.print()函数格式化输出-超详解

    Python·.·.print()函数格式化输出 print()函数格式为: print(objects, sep=' ', end='\n', file=sys.stdout) 文章目录 Pytho ...

  3. Python print函数及format()格式化输出详解

    今天继续给大家介绍Python相关知识,本文主要内容是Python print函数及format()格式化输出详解. 一.print()函数基本使用 print()函数是Python中用于向控制台输出 ...

  4. python print函数用法_Python3.2中Print函数用法实例详解

    本文实例讲述了Python3.2中Print函数用法.分享给大家供大家参考.具体分析如下: 1. 输出字符串 >>> strHello = 'Hello World' >> ...

  5. Python print函数用法,print 格式化输出

    使用print输出各型的 字符串 整数 浮点数 出度及精度控制 strHello = 'Hello Python' print strHello #输出结果:Hello Python #直接出字符串 ...

  6. 用 python print() 函数实现的三个特效

    print() 应该是初学者最先接触到的第一个 Python 函数,因为几乎所有的启蒙课程都是从 print('Hello world') 开始的.事实上, print() 也是程序员使用频率最高的函 ...

  7. Python——print()函数的学习笔记

    1 致谢 感谢高老师的讲解! 2 前言 今天在学习Python编程~ 3 print()函数的使用 print()函数使用{}花括号作为占位符: 可以看看print()函数这种输出的方式,

  8. Python Print函数用法

    Pythonprint函数用法,print格式化输出 使用print输出各型的 字符串 整数 浮点数 出度及精度控制 strHello = 'Hello Python' print strHello ...

  9. python print函数详细用法

    文章目录 输出变量 sep参数 end参数 file参数 flush参数   先提前说,我这篇文章很水,是我用了很久Python,却发现连最常用的print方法都不熟悉,这个方法竟然还有四个参数!这是 ...

最新文章

  1. ajax post请求,后台servlet来接收
  2. 这样玩《TmoLand》包你收益最大化
  3. 常用Nagios配置命令
  4. Linux下串口编程入门
  5. python如何打印26个字母_玩法#用Python学会打印26个英语字母,简单操作如下
  6. Asp.net MVC2.0系列文章-编辑和删除新闻操作
  7. 曼哈顿距离和欧拉距离
  8. CSS边框,背景,边距,溢出
  9. java 内存分布_一图看懂JVM内存分布,永久记住!
  10. 完整学习git五git重置
  11. __FILE__,__LINE__,FUNCTION__实现代码跟踪调试(linux下c语言编程 )(转自IT博客)
  12. Elasticsearch——filter过滤查询
  13. 20169210《Linux内核原理与分析》课程总结
  14. Pycharm TensorFolw配置
  15. 数据库工作笔记008---pg_hba.conf entry for host “192.168.2.111”, user “gpadmin”, database “template1”, SSL
  16. JsonHelper(Json帮助类)
  17. 面试突击41:notify是随机唤醒吗?
  18. C# 图片exif信息
  19. bt4 linux 镜像,让你的启动U盘集合7个系统包括pe bt4 beibi q3 linux dos等
  20. Google Maps Download Tool 谷歌地图下载工具

热门文章

  1. windows下安装Python和安装Python解释器
  2. 绕过磊科路由器登录密码
  3. js返回一年中所有的自然周
  4. 牛逼,《2020 年全球程序员收入报告》,字节跳动高居全球第七
  5. 什么是IDaaS?IDaaS有什么应用意义?
  6. 共识机制-区块链核心技术之一
  7. 实现扫描图片出现3d模型的虚拟仿真技术
  8. 【小沐学NLP】Python实现聊天机器人(ALICE)
  9. Linux内核的5个子系统
  10. np.arange与np.linspace细微区别(数据溢出问题)