如何强制Python的打印功能输出到屏幕?
这不是禁用输出缓冲的重复- 链接的问题是尝试无缓冲输出,而这是更一般的.这个问题的最佳答案太强大或涉及到这个问题(他们不是很好的答案),这个问题可以在谷歌上找到一个相对新手.
我想做几个语句,给出标准输出,而不会在语句之间看到换行符.
具体来说,假设我有:
for item in range(1,100):
print item
Run Code Online (Sandbox Code Playgroud)
结果是:
1
2
3
4
.
.
.
Run Code Online (Sandbox Code Playgroud)
如何让它看起来像:
1 2 3 4 5 ...
Run Code Online (Sandbox Code Playgroud)
更妙的是,是否可以打印单号在最后一个号码,所以只有一个号码在屏幕上在同一时间?
在python中,如果我说
print 'h'
Run Code Online (Sandbox Code Playgroud)
我收到了字母h和换行符.如果我说
print 'h',
Run Code Online (Sandbox Code Playgroud)
我收到了字母h而没有换行.如果我说
print 'h',
print 'm',
Run Code Online (Sandbox Code Playgroud)
我收到了字母h,空格和字母m.如何防止Python打印空间?
print语句是同一循环的不同迭代,所以我不能只使用+运算符.
我今天开始编程并且在Python中遇到了这个问题.这是相当愚蠢但我无法弄清楚如何做到这一点.当我使用print命令时,它会打印我想要的任何内容,然后转到另一行.例如:
print "this should be"; print "on the same line"
Run Code Online (Sandbox Code Playgroud)
应该返回:
这应该在同一条线上
而是返回:
这应该
在同一条线上
更确切地说,我试图创建一个程序if
,告诉我一个数字是否是2
def test2(x):
if x == 2:
print "Yeah bro, that's tottaly a two"
else:
print "Nope, that is not a two. That is a (x)"
Run Code Online (Sandbox Code Playgroud)
但是它不会将最后一个识别(x)
为输入的值,而是精确打印:"(x)"(带括号的字母).为了使它工作,我必须写:
print "Nope, that is not a two. That is a"; print (x)
Run Code Online (Sandbox Code Playgroud)
如果我输入test2(3)
它给出:
不,那不是两个,就是
3
所以要么我需要让Python在打印行中识别我的(x)作为数字; 或打印两个单独的东西,但在同一条线上.在此先感谢并抱歉这样一个愚蠢的问题.
重要说明:我使用的是2.5.4版
另一个注意事项:如果我print "Thing" , print "Thing2"
在第二次打印时说"语法错误".
我有这个代码:
>>> for i in xrange(20):
... print 'a',
...
a a a a a a a a a a a a a a a a a a a a
Run Code Online (Sandbox Code Playgroud)
我想输出'a'
,' '
不像这样:
aaaaaaaaaaaaaaaaaaaa
Run Code Online (Sandbox Code Playgroud)
可能吗?
我想将循环输出打印到同一行的屏幕上.
我如何以最简单的方式处理Python 3.x.
我知道这个问题已经被要求用于Python 2.7,在行的末尾使用逗号即打印I,但我找不到Python 3.x的解决方案.
i = 0
while i <10:
i += 1
## print (i) # python 2.7 would be print i,
print (i) # python 2.7 would be 'print i,'
Run Code Online (Sandbox Code Playgroud)
屏幕输出.
1
2
3
4
5
6
7
8
9
10
Run Code Online (Sandbox Code Playgroud)
我想要打印的是:
12345678910
Run Code Online (Sandbox Code Playgroud)
新读者访问此链接以及http://docs.python.org/release/3.0.1/whatsnew/3.0.html
我读到在打印语句后禁止换行,你可以在文本后面加一个逗号.这里的示例看起来像Python 2. 如何在Python 3中完成?
例如:
for item in [1,2,3,4]:
print(item, " ")
Run Code Online (Sandbox Code Playgroud)
需要更改哪些内容才能将它们打印在同一行上?
此代码来自http://docs.python.org/2/tutorial/errors.html#predefined-clean-up-actions
with open("myfile.txt") as f:
for line in f:
print line,
Run Code Online (Sandbox Code Playgroud)
我不明白的是,
打印命令结束时的情况.
我还检查了doc,http://docs.python.org/2/library/functions.html#print.
不够了解,这是一个错误吗?(似乎不是.它来自官方教程).
我来自ruby/javascript,这对我来说很不寻常.
我试图printf()
在Linux上的python命令行中使用C函数.为了完成这项工作,我导入了ctypes
.我的问题是:如果我创建一个在循环中CDLL
使用printf()
-function 的对象,我得到一个非常奇怪的输出:
>>> import ctypes
>>> libc = ctypes.CDLL("libc.so.6")
>>> for i in range(10):
... libc.printf("%d", i)
...
01
11
21
31
41
51
61
71
81
91
>>>
Run Code Online (Sandbox Code Playgroud)
但是,当我在函数内部调用此循环时,它按预期工作:
>>> import ctypes
>>> libc = ctypes.CDLL("libc.so.6")
>>> def pr():
... for i in range(10):
... libc.printf("%d", i)
... libc.printf("\n")
...
>>> pr()
0123456789
>>>
Run Code Online (Sandbox Code Playgroud)
我猜不出是什么导致了这种行为......
如果重要的话,我在Linux上使用Python 2.7.6.
编辑:
Python版本/操作系统对此没有影响.有关详细信息,请参阅下面的PM 2Ring的答案.在Windows上,您只需将初始化更改libc
为libc = ctypes.CDLL("msvcrt.dll")
where .dll
是可选的.获取正确输出比调用函数的另一种方法是将printf()
变量值存储在变量中:
>>> …
Run Code Online (Sandbox Code Playgroud) 我想知道在打印东西时如何删除额外的空格.
就像我这样做:
print 'Value is "', value, '"'
Run Code Online (Sandbox Code Playgroud)
输出将是:
Value is " 42 "
Run Code Online (Sandbox Code Playgroud)
但我想要:
Value is "42"
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
python ×10
printing ×6
python-2.x ×3
python-3.x ×3
ctypes ×1
flush ×1
formatting ×1
newline ×1
python-2.5 ×1
python-2.7 ×1
string ×1
whitespace ×1