Python print end = ''

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=' ') 

end='' - invalid syntax. I am curious why and what, perhaps, the author wanted to do.

I'm new to python, if you haven't guessed it yet.




I think the main cause of the problem is that these import failures and therefore it should 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 * 
+105
python
Mar 16 '10 at 16:26
source share
13 answers

Are you sure you are using Python 3.x? Syntax is not available in Python 2.x because print is still an expression.

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

in Python 2.x is identical

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

or

 print "foo" % bar, end=" " 

i.e. as calling print with a tuple as an argument.

This is clearly bad syntax (literals do not accept keyword arguments). In Python 3.x, print is a valid function, so it also takes keyword arguments.

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

 print "foo" % bar, 

(pay attention to the final comma, this makes it end the line with a space, not a line)

If you want more control over your output, consider using sys.stdout directly. This will not do any special magic with the exit.

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

 from __future__ import print_function 

The same thing happens with unicode_literals and some other nice things (like with_statement ). This will not work in really old versions (i.e., Created before the function was introduced) Python 2.x.

+142
Mar 16
source share

How about this:

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

This allows you to use the print style function of Python 3.0 without having to manually edit all print entries :)

+47
Mar 16 '10 at 16:28
source share

In Python 2.7, this is how you do it

 mantra = 'Always look on the bright side of life' for c in mantra: print c, #output A lwayslookonthebrights ideoflife 

In python 3.x

 myjob= 'hacker' for c in myjob: print (c, end=' ') #output hacker 
+15
Jul 29 '15 at
source share

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

In Python 3.x, the end=' ' will put a space after the displayed line instead of a new line. To do the same in Python 2.x, you put a comma at the end:

 print "Building internam Index for %d tile(s) ..." % len(inputTiles), 
+5
Mar 16
source share

I think it uses Python 3.0 and you are using Python 2.6.

+4
Mar 16 '10 at 16:30
source share

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

end=' ' just say that you need space after the end of the instruction instead of a new line character. In Python 2.x, you have to do this by placing a comma at the end of the print statement. A.

For example, when in Python 3.x:

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

It produces the following result:

 0 1 2 3 4 

Where how:

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

Gives as output:

 0 1 2 3 4 
+3
Jun 11 '15 at 23:32
source share

It looks like you just don't have a double opening quote. Try:

 if Verbose: print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ') 
+2
Mar 16 '10 at 16:30
source share

I think the author probably meant:

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

He missed the initial quote after print( .

Note that with Python 3.0 , print is a function as opposed to a statement if you are using older versions of Python equivalent:

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

The end parameter means that the string receives a ' ' at the end, not a newline character. Equivalent in earlier versions of Python:

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

(thanks Ignacio).

+1
Mar 16 '10 at 16:28
source share

For python 2.7 I had the same problem Just use " from __future__ import print_function " without quotes to solve this problem. This provides Python 2.6 and later. Python 2.x can use the print function of Python 3.x.

+1
Mar 07 '17 at 3:14
source share

Try this if you are working with python 2.7:

 from __future__ import print_function 
+1
Dec 21 '17 at 7:58
source share

Even today I was getting the same error. And I experienced an interesting thing. If you use python 3.x and still get the error, this may be the reason:

You have several versions of python installed on the same drive. And when you assign the f5 button in the python shell window (from ver. <3.x) pops up

Today I was getting the same error and noticed this thing. Believe me, when I execute my code from the corresponding shell window (version 3.x), I got satisfactory results

0
Jan 06 '13 at 18:28
source share

USE :: python3 filename.py

I had such an error, this happened because I have two versions of python installed on my disk, namely python2.7 and python3. The following is my code:

 #!usr/bin/python f = open('lines.txt') for line in f.readlines(): print(line,end ='') 

when I run it with the 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 with python3 lines.py , I successfully executed

0
Jul 05 '13 at 17:32
source share

we need to import the header before using end='' , since it is not included in the usual python runtime.

 from __future__ import print_function 

it works fine now

0
Jun 10 '14 at 12:30
source share



All Articles