问题:python print end =”

我有这个需要运行的python脚本 gdal_retile.py

但是我在这条线上有一个exceptions:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

end=''是无效的语法。我很好奇为什么以及作者可能打算做什么。

如果您还没猜到,我是python的新手。


我认为问题的根本原因是这些导入失败,因此必须包含此导入 from __future__ import print_function

try: 
   from osgeo import gdal
   from osgeo import ogr
   from osgeo import osr
   from osgeo.gdalconst import *
except:
   import gdal
   import ogr
   import osr
   from gdalconst import *

I have this python script where I need to run gdal_retile.py, but I get an exception on this line:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

The end=' ' is invalid syntax. I am curious as to why, and what the author probably meant to do.

I’m new to python if you haven’t already guessed.


I think the root cause of the problem is that these imports are failing and therefore one must contain this import from __future__ import print_function

try: 
   from osgeo import gdal
   from osgeo import ogr
   from osgeo import osr
   from osgeo.gdalconst import *
except:
   import gdal
   import ogr
   import osr
   from gdalconst import *

回答 0

您确定使用的是Python 3.x吗?该语法在Python 2.x中不可用,因为print它仍然是一条语句。

print("foo" % bar, end=" ")

在Python 2.x中与

print ("foo" % bar, end=" ")

要么

print "foo" % bar, end=" "

即作为调用以元组为参数进行打印。

显然这是错误的语法(文字不带关键字参数)。在Python 3.x print中,它是一个实际函数,因此它也带有关键字参数。

Python 2.x中正确的习惯用法end=" "是:

print "foo" % bar,

(请注意最后一个逗号,这使它以空格而不是换行符结束)

如果要进一步控制输出,请考虑sys.stdout直接使用。这不会对输出产生任何特殊的影响。

当然,在最新版本的Python 2.x(2.5应该有它,不确定是2.4)中,您可以使用__future__模块在脚本文件中启用它:

from __future__ import print_function

这同样与unicode_literals和其他一些好东西(with_statement等)。但是,这在真正的旧版本(即在引入此功能之前创建)Python 2.x中不起作用。

Are you sure you are using Python 3.x? The syntax isn’t available in Python 2.x because print is still a statement.

print("foo" % bar, end=" ")

in Python 2.x is identical to

print ("foo" % bar, end=" ")

or

print "foo" % bar, end=" "

i.e. as a call to print with a tuple as argument.

That’s obviously bad syntax (literals don’t take keyword arguments). In Python 3.x print is an actual function, so it takes keyword arguments, too.

The correct idiom in Python 2.x for end=" " is:

print "foo" % bar,

(note the final comma, this makes it end the line with a space rather than a linebreak)

If you want more control over the output, consider using sys.stdout directly. This won’t do any special magic with the output.

Of course in somewhat recent versions of Python 2.x (2.5 should have it, not sure about 2.4), you can use the __future__ module to enable it in your script file:

from __future__ import print_function

The same goes with unicode_literals and some other nice things (with_statement, for example). This won’t work in really old versions (i.e. created before the feature was introduced) of Python 2.x, though.


回答 1

这个怎么样:

#Only for use in Python 2.6.0a2 and later
from __future__ import print_function

这使您可以使用Python 3.0样式print功能,而无需手动编辑所有出现的print:)

How about this:

#Only for use in Python 2.6.0a2 and later
from __future__ import print_function

This allows you to use the Python 3.0 style print function without having to hand-edit all occurrences of print :)


回答 2

在python 2.7中,这是您的操作方式

mantra = 'Always look on the bright side of life'
for c in mantra: print c,

#output
A l w a y s   l o o k   o n   t h e   b r i g h t   s i d e   o f   l i f e

在python 3.x中

myjob= 'hacker'
for c in myjob: print (c, end=' ')
#output 
h a c k e r 

In python 2.7 here is how you do it

mantra = 'Always look on the bright side of life'
for c in mantra: print c,

#output
A l w a y s   l o o k   o n   t h e   b r i g h t   s i d e   o f   l i f e

In python 3.x

myjob= 'hacker'
for c in myjob: print (c, end=' ')
#output 
h a c k e r 

回答 3

首先,您在开头缺少引号,但这可能是复制/粘贴错误。

在Python 3.x中,end=' '零件将在显示的字符串之后而不是换行符后放置一个空格。要在Python 2.x中执行相同的操作,请在末尾加逗号:

print "Building internam Index for %d tile(s) ..." % len(inputTiles),

First of all, you’re missing a quote at the beginning but this is probably a copy/paste error.

In Python 3.x, the end=' ' part will place a space after the displayed string instead of a newline. To do the same thing in Python 2.x, you’d put a comma at the end:

print "Building internam Index for %d tile(s) ..." % len(inputTiles),

回答 4

我认为他正在使用Python 3.0,而您正在使用Python 2.6。

I think he’s using Python 3.0 and you’re using Python 2.6.


回答 5

这只是一个版本。由于Python 3.x的print实际上是一个函数,因此它现在像任何普通函数一样接受参数。

end=' '仅仅是说,你要声明,而不是一个新行字符结束后的空间。在Python 2.x中,您必须通过在print语句的末尾放置一个逗号来做到这一点。

例如,在Python 3.x环境中:

while i<5:
    print(i)
    i=i+1

将给出以下输出:

0
1
2
3
4

如:

while i<5:
    print(i, end = ' ')
    i=i+1

将给出作为输出:

0 1 2 3 4

This is just a version thing. Since Python 3.x the print is actually a function, so it now takes arguments like any normal function.

The end=' ' is just to say that you want a space after the end of the statement instead of a new line character. In Python 2.x you would have to do this by placing a comma at the end of the print statement.

For example, when in a Python 3.x environment:

while i<5:
    print(i)
    i=i+1

Will give the following output:

0
1
2
3
4

Where as:

while i<5:
    print(i, end = ' ')
    i=i+1

Will give as output:

0 1 2 3 4

回答 6

看来您只是缺少一个开双引号。尝试:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

It looks like you’re just missing an opening double-quote. Try:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

回答 7

我认为作者可能意味着:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

他在之后缺少首字母报价print(

请注意,从Python 3.0开始,它print是与语句相对的函数,如果您使用的是旧版Python,则等效项为:

print "Building internam Index for %d tile(s) ..." % len(inputTiles)

end参数表示该行' '位于末尾,而不是换行符。Python的早期版本中的等效项是:

print "Building internam Index for %d tile(s) ..." % len(inputTiles),

(感谢Ignacio)。

I think the author probably meant:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

He’s missing an initial quote after print(.

Note that as of Python 3.0, print is a function as opposed to a statement, if you’re using older versions of Python the equivalent would be:

print "Building internam Index for %d tile(s) ..." % len(inputTiles)

The end parameter means that the line gets ' ' at the end rather than a newline character. The equivalent in earlier versions of Python is:

print "Building internam Index for %d tile(s) ..." % len(inputTiles),

(thanks Ignacio).


回答 8

用 :: python3 filename.py

我有这样的错误,是因为我在驱动器上安装了两个版本的python,即python2.7和python3。以下是我的代码:

#!usr/bin/python

f = open('lines.txt')
for line in f.readlines():
        print(line,end ='')

当我通过命令运行它时,python lines.py出现以下错误

#!usr/bin/python

f = open('lines.txt')
for line in f.readlines():
        print(line,end ='')

当我通过命令python3 lines.py成功运行它时

USE :: python3 filename.py

I had such error , this occured because i have two versions of python installed on my drive namely python2.7 and python3 . Following was my code :

#!usr/bin/python

f = open('lines.txt')
for line in f.readlines():
        print(line,end ='')

when i run it by the command python lines.py I got the following error

#!usr/bin/python

f = open('lines.txt')
for line in f.readlines():
        print(line,end ='')

when I run it by the command python3 lines.py I executed successfully


回答 9

对于python 2.7,我有相同的问题,只需使用“ from __future__ import print_function ”不带引号即可解决此问题。这确保Python 2.6和更高版本的Python 2.x可以使用Python 3.x打印功能。

For python 2.7 I had the same issue Just use “from __future__ import print_function” without quotes to resolve this issue.This Ensures Python 2.6 and later Python 2.x can use Python 3.x print function.


回答 10

如果您使用的是python 2.7,请尝试以下一项:

from __future__ import print_function

Try this one if you are working with python 2.7:

from __future__ import print_function

回答 11

甚至我今天也遇到同样的错误。我经历了一件有趣的事情。如果您使用的是python 3.x并且仍然收到错误,则可能是原因:

您在同一驱动器上安装了多个python版本。当您按f5按钮时,将弹出python shell窗口(版本<3.x)

今天我遇到了同样的错误,并注意到了那件事。相信我,当我从适当的Shell窗口(版本3.x)执行代码时,我得到了令人满意的结果

Even I was getting that same error today. And I’ve experienced an interesting thing. If you’re using python 3.x and still getting the error, it might be a reason:

You have multiple python versions installed on same drive. And when you’re presing the f5 button the python shell window (of ver. < 3.x) pops up

I was getting same error today, and noticed that thing. Trust me, when I execute my code from proper shell window (of ver. 3.x), I got satisfactory results


回答 12

我们需要在使用之前导入标头end='',因为标头不包含在python的正常运行时中。

from __future__ import print_function

现在应该可以正常工作了

we need to import a header before using end='', as it is not included in the python’s normal runtime.

from __future__ import print_function

it shall work perfectly now


回答 13

兼容两者 的Python 2和3

sys.stdout.write('mytext')

仅与Python 2兼容

print 'mytext',

仅与Python 3兼容

print('mytext', end='')

Compatible with both Python 2 & 3:

sys.stdout.write('mytext')

Compatible with only Python 2

print 'mytext',

Compatible with only Python 3

print('mytext', end='')

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。