[英]Python3 Print Function
This probably an easy question but I just can't seem to make it work consistently for different permutations. 这可能是一个简单的问题,但我似乎无法使它针对不同的排列一致地工作。
What is the Python3 equivalent of this? Python3等效于什么?
print >> sys.stdout, "Generated file: %s in directory %s\n" % (name+".wav", outdir)
I've tried 我试过了
print("String to print %s %s\n", file=sys.stdout % (var1, var2))
and 和
print("String to print {} {}\n".format(var1, var2), file=sys.stdout)
What is the best way to do this in Python3 now that the >> operator is no more. 既然>>运算符不再可用,那么在Python3中执行此操作的最佳方法是什么。 I know the % () has the be within the closing parenthesis of the print function but I always have trouble when using formatting as well as printing to a specific file/stream at the same time.
我知道%()在print函数的右括号内,但是在使用格式设置以及同时打印到特定文件/流时,我总是遇到麻烦。
Put the percent right next to the end of the string literal. 将百分比放在字符串文字的末尾。
print("String to print %s %s\n" % (var1, var2), file=sys.stdout)
The percent operator is just like any other binary operator. 百分比运算符与其他任何二进制运算符一样。 It goes between the two things it's operating on -- in this case, the format string and the format arguments.
它介于正在处理的两件事之间—在这种情况下,是格式字符串和格式参数。 It's the same reason that
print(2+2, file=sys.stdout)
works and print(2, file=sys.stdout + 2)
doesn't. 这是
print(2+2, file=sys.stdout)
起作用而print(2, file=sys.stdout + 2)
不能起作用的相同原因。
(personal opinion corner: I think format
is way better than percent style formatting, so you should just use your third code block, which behaves properly as you've written it) (个人观点:我认为
format
比百分比样式格式好得多,因此您应该只使用第三个代码块,该代码块在编写时会正常工作)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.