Solution to: [Beginner question] Raster to ASCII conversion

MRAB python at mrabarnett.plus.com
Mon Nov 15 15:37:00 EST 2010


On 15/11/2010 20:14, Becky Kern wrote:
> Hi users,
> Found a solution to my Raster to ASCII conversion problem, so I thought
> I'd post.
> My raster was created in ArcGIS 9.3 as an ESRI GRID (folder with
> associated info folder). I needed to use the ListRasters method (see
> http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?id=958&pid=905&topicname=ListRasters_method
> <http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?id=958&pid=905&topicname=ListRasters_method>) to
> get Python to recognize it as a raster. My functioning code is posted below.
>  >>import arcgisscripting
>  >>gp = arcgisscripting.create(9.3)
>  >>gp.workspace = "C:\Users\Lab
> User\Desktop\BKern\Blackwater\PVA\Scenario 1\Nov 15"
>  >>rasters = gp.ListRasters("","ALL")
>  >>for raster in rasters: print raster
> ...
> scen1_11_15
>  >>InRaster = "scen1_11_15"
>  >>OutasciiFile = "C:\Users\Lab
> User\Desktop\BKern\Blackwater\PVA\Scenario 1\Nov 15\scen1.asc"
>  >>gp.RasterToASCII_conversion(InRaster, OutasciiFile)
>
Be careful when using string literals containing backslashes. It's
recommended that you use raw strings:

     gp.workspace = r"C:\Users\Lab 
User\Desktop\BKern\Blackwater\PVA\Scenario 1\Nov 15"

     OutasciiFile = r"C:\Users\Lab 
User\Desktop\BKern\Blackwater\PVA\Scenario 1\Nov 15\scen1.asc"

You were lucky that none of the backslashes started an escape sequence.
If you were using Python 3 then it would've complained about \U and \N.



More information about the Python-list mailing list