Phython Script TypeError: range() integer end argument expected, got float

2144
4
Jump to solution
02-20-2021 03:27 AM
RIKAHARUYAMA
New Contributor II

Hello. I was trying to copy the shape features a number of times based on the numeric field by using the python code(The page referenced: <https://community.esri.com/t5/geoprocessing-questions/copy-features-a-number-of-times-based-on-a-num...>@ XanderBakker)

---

def main():
    import arcpy
    import os

    fc_in = r"D:\forest\CHISAN\shp\L_koshibugawa.shp" # this one exists
    fld_count = "Count"
    fc_out = r"D:\forest\CHISAN\shp\L_koshibugawa_duplicate.shp" # this one will be created
    sr = arcpy.Describe(fc_in).spatialReference

    # create the empty output featureclass
    path, name = os.path.split(fc_out)
    arcpy.CreateFeatureclass_management(path, name, "POLYLINE", fc_in, "SAME_AS_TEMPLATE", "SAME_AS_TEMPLATE", sr)

    # insert the features into the output fc
    with arcpy.da.SearchCursor(fc_in, '*') as curs_in:
        flds_in = curs_in.fields
        idx_cnt = flds_in.index(fld_count)
        with arcpy.da.InsertCursor(fc_out, '*') as curs_out:
            for row in curs_in:
                cnt = row[idx_cnt]
                for i in range(0, cnt):
                    curs_out.insertRow(row)

if __name__ == '__main__':
    main()

---

The module was able to run, but I it got the error message below.

Traceback (most recent call last):
  File "D:\forest\test.py", line 31, in <module>
    main()
  File "D:\forest\test.py", line 25, in main
    for i in range(0, cnt):
TypeError: range() integer end argument expected, got float.

 

Would be super nice if someone could lectuer me how to solve this problem.

Thank you.

 

 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

I would say it's a simple as your COUNT field being of FLOAT type.  Check this, and if so, use Type Conversion to turn it into a integer:

for i in range(0, int(cnt))

I've got no idea if the rest of the code is even valid as it gave me a headache.

View solution in original post

4 Replies
DavidPike
MVP Frequent Contributor

I would say it's a simple as your COUNT field being of FLOAT type.  Check this, and if so, use Type Conversion to turn it into a integer:

for i in range(0, int(cnt))

I've got no idea if the rest of the code is even valid as it gave me a headache.

RIKAHARUYAMA
New Contributor II

The module run successfully after I fixed the field type from "float" to "integer".

Much Appliciated 🙂

0 Kudos
DanPatterson
MVP Esteemed Contributor

your field is incorrect 

fld_count = "Count"

check to make sure that it represents a true count because if it contains decimal floats like 1.5 you are going to have more issues.

As for formatting

Code formatting ... the Community Version - GeoNet, The Esri Community


... sort of retired...
DavidPike
MVP Frequent Contributor
with arcpy.da.SearchCursor(fc_in, '*') as curs_in:
        flds_in = curs_in.fields
        idx_cnt = flds_in.index(fld_count)

So I think flds_in is a tuple of the cursor fields, then the .index() method is used effectively as .index("COUNT"), to be able to grab the row[] by index (i.e. the value of the COUNT field at that row).

that leads me to think that the COUNT field is float type as per the error, although what is actually going on in the code and I have little idea.