Posts: 6
Threads: 2
Joined: Oct 2018
hi all..
sorry if i reposted this question..
so i have code like this from Xander Bakker:
def main():
cnt = 0
for i in range(25):
cnt += 1
km, hect = divmod(cnt, 10)
hect_txt = "%03d" % (hect * 100,)
lbl = "{}+{}".format(km, hect_txt)
print("{}: {}".format(cnt, lbl))
if __name__ == '__main__':
main() so the script will show result like this:
0+100
0+200
...
0+900
1+000
1+100
and so on..
if i want to start with 0+000, what is the code i should add or modify from the script?
thanks
Posts: 4,449
Threads: 68
Joined: Jan 2018
Perhaps initialize with cnt = -1 instead of cnt = 0
Posts: 8,085
Threads: 153
Joined: Sep 2016
def main():
for cnt in range(25):
km, hect = divmod(cnt, 10)
hect_txt = "%03d" % (hect * 100,)
lbl = "{}+{}".format(km, hect_txt)
print("{}: {}".format(cnt, lbl))
if __name__ == '__main__':
main() Output: 0: 0+000
1: 0+100
2: 0+200
3: 0+300
4: 0+400
5: 0+500
6: 0+600
7: 0+700
8: 0+800
9: 0+900
10: 1+000
11: 1+100
12: 1+200
13: 1+300
14: 1+400
15: 1+500
16: 1+600
17: 1+700
18: 1+800
19: 1+900
20: 2+000
21: 2+100
22: 2+200
23: 2+300
24: 2+400
>>>
Posts: 6
Threads: 2
Joined: Oct 2018
(Nov-15-2018, 11:08 AM)buran Wrote: def main():
for cnt in range(25):
km, hect = divmod(cnt, 10)
hect_txt = "%03d" % (hect * 100,)
lbl = "{}+{}".format(km, hect_txt)
print("{}: {}".format(cnt, lbl))
if __name__ == '__main__':
main() Output: 0: 0+000
1: 0+100
2: 0+200
3: 0+300
4: 0+400
5: 0+500
6: 0+600
7: 0+700
8: 0+800
9: 0+900
10: 1+000
11: 1+100
12: 1+200
13: 1+300
14: 1+400
15: 1+500
16: 1+600
17: 1+700
18: 1+800
19: 1+900
20: 2+000
21: 2+100
22: 2+200
23: 2+300
24: 2+400
>>>
so if i have code like this
def main():
import arcpy
fc = r'Drive:\Path\To\Source\FC'
fld_seq = 'SequenceLbl'
# Add field if it does not exist already
AddField(fc, fld_seq, "TEXT", 12)
# start update cursor to update the values in the new field
flds = (fld_seq)
cnt = 0
with arcpy.da.UpdateCursor(fc, flds) as curs:
for row in curs:
cnt += 1
km, hect = divmod(cnt, 10)
hect_txt = "%03d" % (hect * 100,)
lbl = "{}+{}".format(km, hect_txt)
print cnt, hect_txt
curs.updateRow((lbl, ))
def AddField(tbl, fld_name, fld_type, fld_length):
if len(arcpy.ListFields(tbl, fld_name)) == 0:
arcpy.AddField_management(tbl, fld_name, fld_type, None, None, fld_length)
if __name__ == '__main__':
main() where should i put the for cnt in range(25): ?
btw thanks for the help
Posts: 8,085
Threads: 153
Joined: Sep 2016
Nov-15-2018, 11:28 AM
(This post was last modified: Nov-15-2018, 11:28 AM by buran.)
you don't need that line. you iterate over rows in cursor and update the currentRow.
No tested but I think you should
def main():
import arcpy
fc = r'Drive:\Path\To\Source\FC'
fld_seq = 'SequenceLbl'
# Add field if it does not exist already
AddField(fc, fld_seq, "TEXT", 12)
# start update cursor to update the values in the new field
flds = (fld_seq)
with arcpy.da.UpdateCursor(fc, flds) as curs:
for cnt, row in enumerate(curs):
km, hect = divmod(cnt, 10)
hect_txt = "%03d" % (hect * 100,)
lbl = "{}+{}".format(km, hect_txt)
print cnt, hect_txt
curs.updateRow((lbl, ))
def AddField(tbl, fld_name, fld_type, fld_length):
if not arcpy.ListFields(tbl, fld_name):
arcpy.AddField_management(tbl, fld_name, fld_type, None, None, fld_length) Check if it does what you want. I am not sure if you need to supplu just lbl or the entire row
Here is link to docs http://pro.arcgis.com/en/pro-app/arcpy/d...-class.htm
Posts: 6
Threads: 2
Joined: Oct 2018
(Nov-15-2018, 11:26 AM)buran Wrote: you don't need that line. you iterate over rows in cursor and update the currentRow.
No tested but I think you should
ef main():
import arcpy
fc = r'Drive:\Path\To\Source\FC'
fld_seq = 'SequenceLbl'
# Add field if it does not exist already
AddField(fc, fld_seq, "TEXT", 12)
# start update cursor to update the values in the new field
flds = (fld_seq)
with arcpy.da.UpdateCursor(fc, flds) as curs:
for cnt, row in enumerate(curs):
km, hect = divmod(cnt, 10)
hect_txt = "%03d" % (hect * 100,)
lbl = "{}+{}".format(km, hect_txt)
print cnt, hect_txt
curs.updateRow((lbl, ))
def AddField(tbl, fld_name, fld_type, fld_length):
if len(arcpy.ListFields(tbl, fld_name)) == 0:
arcpy.AddField_management(tbl, fld_name, fld_type, None, None, fld_length) Check if it does what you want. I am not sure if you need to supplu just lbl or the entire row
Here is link to docs http://pro.arcgis.com/en/pro-app/arcpy/d...-class.htm
ok thanks Mr Buran
very helpful
|