简体   繁体   English

Python textwrap:终端打印与写入文件的不同结果

[英]Python textwrap: different results for terminal print vs. write to file

I have been experimenting with the Python textwrap module, to wrap long lines of text.我一直在尝试使用 Python textwrap 模块来换行长文本。 I noticed that the print output to terminal looks fine, the word wrap works as expected.我注意到打印到终端的 output 看起来不错,自动换行按预期工作。 But when I write to file, slightly different output is seen.但是当我写入文件时,看到的 output 略有不同。 Is this a known feature of print vs. write, I wonder?我想知道这是打印与写入的已知功能吗?

import os, sys
import textwrap

INFILE="in.txt"
OUTFILE="out.txt"

with open(INFILE, 'r') as file:
  lines = file.readlines()
  for line in lines:
    print(textwrap.fill(line, width=42))
    # works as expected

with open(OUTFILE, 'w') as fp:
 for l in lines:
   fp.write(textwrap.fill(l, width=42))
   # not the same output is written to file

in.txt:在.txt:

As a special exception to the GNU Lesser General Public License version 3 ("LGPL3"), the copyright holders of this Library give you permission to convey to a third party a Combined Work that links statically or dynamically to this Library without providing any Minimal Corresponding Source or Minimal Application Code as set out in 4d or providing the installation information set out in section 4e, provided that you comply with the other provisions of LGPL3 and provided that you meet, for the Application the terms and conditions of the license(s) which apply to the Application.

GNU LESSER GENERAL PUBLIC LICENSE

Version 3, 29 June 2007

Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.

This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.

I ended up using this code.我最终使用了这段代码。 It uses print() and sends the output to a file and works well.它使用 print() 并将 output 发送到文件并且运行良好。

       print("Wrapping lines..."+myfile) 
       for l in lines:
           print(textwrap.fill(l, width=80),file=fp)```


Yes, there is a difference between print() and write() .是的, print()write()是有区别的。 Several: Here they are:几个:他们在这里:

  • print()
    • is a built-in global function是内置的全局function
    • by default outputs to the stdout (but can be changed with the fp= arg).默认输出到stdout (但可以使用fp= arg 更改)。
    • by default adds a newline ( \n ) to each string (but can be changed with the end= arg).默认情况下向每个字符串添加一个换行符 ( \n )(但可以使用end= arg 进行更改)。
  • write()
    • is a method (bound function) of a file object是一个文件object的方法(绑定函数)
    • outputs only on that file object仅在该文件 object 上输出
    • does not add a newline;不添加换行符; you have to do that yourself by expressly adding the newline to the string, or inserting an extra fp.write("\n") after your write statement您必须通过明确将换行符添加到字符串或在 write 语句后插入额外的fp.write("\n")来自己完成此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM