python print()函数中的新行 [英] New line in python print() function
问题描述
我正在使用Python 2.7.3,并且正在编写一个脚本,该脚本打印任何用户定义文件的十六进制字节值.它可以正常工作,但有一个问题:每个值都被打印在新的一行上.是否可以使用空格而不是换行来打印值?
I am using Python 2.7.3 and I am writing a script which prints the hex byte values of any user-defined file. It is working properly with one problem: each of the values are being printed on a new line. Is it possible to print the values with spaces instead of new lines?
例如,代替
61
62
我想拥有61 62
.
下面是我的代码(..txt
是一个包含文本'abcd'
的文件):
Below is my code (..txt
is a file which contains the text 'abcd'
):
#!usr/bin/python
import os
import sys
import time
filename = raw_input("Enter directory of the file you want to convert: ")
f = open(filename, 'rb')
fldt = f.read()
lnfl = len(fldt)
print "Length of file is", lnfl, "bytes. "
orck = 0
while orck < lnfl:
bndt = hex(ord(fldt[orck]))
bndt = bndt[-2:]
orck = orck + 1
ent = chr(13) + chr(10)
entx = str(ent)
bndtx = str(bndt)
bndtx.replace(entx, ' ')
print bndtx
推荐答案
首先print
不是Python 2中的函数,它是一条语句.
First of all print
isn't a function in Python 2, it is a statement.
要禁止自动换行符,请在末尾添加,
(逗号).现在将使用空格代替换行符.
To suppress the automatic newline add a trailing ,
(comma). Now a space will be used instead of a newline.
演示:
print 1,
print 2
输出:
1 2
或使用Python 3的 print()
函数:>
Or use Python 3's print()
function:
from __future__ import print_function
print(1, end=' ') # default value of `end` is '\n'
print(2)
您可以清楚地看到print()
函数的功能强大得多,因为我们可以指定要用作end
而不是固定空格的任何字符串.
As you can clearly see print()
function is much more powerful as we can specify any string to be used as end
rather a fixed space.
这篇关于python print()函数中的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!