How to pad strings with zeros...
'''
PaddingStringsDemo.py
'''
vals = [1,10,100,1000]
for i in vals:
out = "%08.2f" % (i)
print str(out)
And this is the output...you will note it is a string representation of the number.
>>>
00001.00
00010.00
00100.00
01000.00
--------------------------------------------------------------------
Parsing data example...the script and the data file must reside in the same folder.
'''
ParsingDataDemo.py
A demo file to parse data which is quasi-fixed width
File and script must reside in the same folder...fix this if you want
'''
import sys, os
data_path = (os.path.dirname(sys.argv[0]) + "/").replace("\\","/") #can be skipped if you follow
data_file = data_path + "ParsingDataDemoData.txt" #fix this or better still create a tool
#data_file should be sys.argv[1] which allows a user to select a file in a folder
a_file = open(data_file)
data = a_file.readlines()
for a_line in data:
record_type = a_line[:3]
parcel_num = a_line[3:11]
the_rest = a_line[11:]
print record_type, parcel_num, the_rest
ParsingDataDemoData.txt
10012345678FIXEDWIDTHDATA
11012345678FIXEDWIDTHBUTLARGERTHAN
12012345678FIXEDWIDTH
10012345678AFIXEDWIDTHDATA
11012345678AFIXEDWIDTHBUTLARGERTHAN
12012345678AFIXEDWIDTH
The result splitting off the the first three numbers, the next 8 and the rest is:
>>>
100 12345678 FIXEDWIDTHDATA
110 12345678 FIXEDWIDTHBUTLARGERTHAN
120 12345678 FIXEDWIDTH
100 12345678 AFIXEDWIDTHDATA
110 12345678 AFIXEDWIDTHBUTLARGERTHAN
120 12345678 AFIXEDWIDTH
--------------------------------------------------------------------
Emulating the $$ROWMAP and $$COLMAP within python that was removed in ArcMap 10.
#Row_Col_rasterDemo.py
'''
source http://forums.arcgis.com/threads/865-quot-built-in-quot-rasters-in-python-map-algebra
Create some rasters of rows and columns
(like old $$ROWMAP and $$COLMAP)
In this example, numpy arrays of 10x10 size and shape are created then converted to ESRI
grids.
'''
import numpy as np
import arcpy
arcpy.env.workspace = "c:/temp"
arcpy.env.overwriteOutput = 1
nprows = np.indices((10,10))[0]
npcols = np.indices((10,10))[1]
# Convert the numpy arrays to ESRI rasters (ie Raster objects)
# called 'rows' and 'cols'
row_ras = arcpy.NumPyArrayToRaster(nprows)
row_ras.save("rowraster")
col_ras = arcpy.NumPyArrayToRaster(npcols)
col_ras.save("colraster")
print row_ras
print col_ras
No comments:
Post a Comment