Python Forum
want to change the beginning of the result
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
want to change the beginning of the result
#1
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
Reply
#2
Perhaps initialize with cnt = -1 instead of cnt = 0
Reply
#3
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 >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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
Reply
#5
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Program doesnt return beginning bilisim19 2 897 Feb-15-2023, 06:23 PM
Last Post: Larz60+
  List of dataframe values beginning with x,y or z glidecode 3 1,890 Nov-08-2021, 10:16 PM
Last Post: glidecode
  How to start the program from the beginning. iamaghost 5 2,873 Feb-23-2021, 03:40 AM
Last Post: deanhystad
  Beginning of Beginner Help: Should I start with Python? appdevelnewb 2 3,000 Jul-23-2018, 11:17 PM
Last Post: appdevelnewb
  Wrap from end to beginning. 27 to 1, 28 to 2 etc DreamingInsanity 5 3,622 Jun-24-2018, 01:02 PM
Last Post: ljmetzger
  How to Loop CSV File Beginning at Specific Row? bmccollum 5 40,244 Nov-05-2017, 11:04 PM
Last Post: bmccollum
  Need help for beginning coding sylas 13 8,158 Mar-28-2017, 06:36 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020