我有这个python脚本,我需要运行 gdal_retile.py
但我在这一行得到一个例外:
if Verbose:
print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')
Run Code Online (Sandbox Code Playgroud)
将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 *
Run Code Online (Sandbox Code Playgroud)
Ala*_*lum 144
你确定你使用的是Python 3.x吗?Python 2.x中没有该语法,因为print它仍然是一个语句.
print("foo" % bar, end=" ")
Run Code Online (Sandbox Code Playgroud)
在Python 2.x中是相同的
print ("foo" % bar, end=" ")
Run Code Online (Sandbox Code Playgroud)
要么
print "foo" % bar, end=" "
Run Code Online (Sandbox Code Playgroud)
即作为以元组作为参数打印的调用.
这显然是错误的语法(文字不接受关键字参数).在Python 3.x print是一个实际的函数,所以它也需要关键字参数.
Python 2.x中的正确习惯end=" "是:
print "foo" % bar,
Run Code Online (Sandbox Code Playgroud)
(注意最后的逗号,这使得它以空格而不是换行符结束)
如果您想要更多地控制输出,请考虑sys.stdout直接使用.这不会对输出做任何特殊的魔术.
当然在最新版本的Python 2.x(2.5应该有它,不确定2.4)中,您可以使用该__future__模块在脚本文件中启用它:
from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)
同样适用于unicode_literals其他一些好东西(with_statement例如).但是,这在Python 2.x的旧版本(即在引入该功能之前创建)中不起作用.
bad*_*adp 47
这个怎么样:
#Only for use in Python 2.6.0a2 and later
from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)
这允许您使用Python 3.0样式print函数,而无需手动编辑所有出现的print:)
Rit*_*rwa 16
在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
Run Code Online (Sandbox Code Playgroud)
在python 3.x中
myjob= 'hacker'
for c in myjob: print (c, end=' ')
#output
h a c k e r
Run Code Online (Sandbox Code Playgroud)
首先,你在开头就错过了一个引用,但这可能是一个复制/粘贴错误.
在Python 3.x中,该end=' '部分将在显示的字符串后面放置一个空格而不是换行符.要在Python 2.x中执行相同的操作,最后会添加一个逗号:
print "Building internam Index for %d tile(s) ..." % len(inputTiles),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
397463 次 |
| 最近记录: |