Python 3.4.1 script syntax error, arcpy &

2019-01-12 12:09发布

问题:

I am used to working in python 2.7 so there was some new things like the print function being different. So excuse my ignorance. I am also pretty new to programming.

So here is my script, I keep getting errors that highlight some commas or spaces and saying there is a

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: malformed \N character escape

Code:

import arcpy

print("mosaic to new raster starting!")

env.workspace = "F:\GDAL"
arcpy.env.pyramid = "NONE"
arcpy.env.rasterStatistics = "NONE"
arcpy.env.compression = "JPEG 87"
arcpy.env.tileSize = "256 256"

print("Environment set")

RasterInput = "m_3511401_ne_11_1_20130731.jpg;m_3511401_nw_11_1_20130731.jpg;m_3511401_se_11_1_20130731.jpg;m_3511401_sw_11_1_20130731.jpg;"

print("Input set")

arcpy.MosaicToNewRaster_management(RasterInput,"F:\Pro_Projects\NAIP2013\raster.sde","MosaicFile1","","8_BIT_UNSIGNED","","3","LAST","FIRST")

print("mosaic done!")

回答1:

Backslashes (used by you as Windows path separators) signal escape sequences in Python strings. Double the backslashes or use a raw string literal:

"F:\\Pro_Projects\\NAIP2013\\raster.sde"

or

r"F:\Pro_Projects\NAIP2013\raster.sde"

Windows also accepts forward slashes in paths, avoiding the issue altogether:

"F:/Pro_Projects/NAIP2013/raster.sde"