solution to struct.error: cannot convert argument to integer

2,435 views
Skip to first unread message

Maciej Kamiński

unread,
Nov 7, 2013, 12:47:07 PM11/7/13
to geospati...@googlegroups.com
I'm asking about lines in shapefile.py

            name = name.replace(b(' '), b('_'))
            name = name.ljust(11).replace(b(' '), b('\x00'))
            fieldType = b(fieldType)
            size = int(size)
            fld = pack('<11sc4xBB14x', name, fieldType, size, decimal)
            f.write(fld)

in code i have
    shp_writer_lines.field('name','N','5','3')

but it throws error

    shp_writer_lines.save('edges')
  File "/usr/lib/python2.7/site-packages/shapefile.py", line 1033, in save
    self.saveDbf(target)
  File "/usr/lib/python2.7/site-packages/shapefile.py", line 1004, in saveDbf
    self.__dbfHeader()
  File "/usr/lib/python2.7/site-packages/shapefile.py", line 748, in __dbfHeader
    fld = pack('<11sc4xBB14x', name, fieldType, size, decimal)
struct.error: cannot convert argument to integer

because 'decimal' is not converted to integer

My question: is it desired to have in my code:
    shp_writer_lines.field('name','N','5',3)
('5' is str 3 is number witch is not obvious from documentation)

or You want something like:

            name = name.replace(b(' '), b('_'))
            name = name.ljust(11).replace(b(' '), b('\x00'))
            fieldType = b(fieldType)
            size = int(size)
            decimal=int(decimal)
            fld = pack('<11sc4xBB14x', name, fieldType, size, decimal)
            f.write(fld)

i'm glad to see the solution.

Gachoud Philippe

unread,
May 29, 2019, 8:58:28 AM5/29/19
to Geospatial Python
Your name variable is a string, you have to pass an int instead!
Reply all
Reply to author
Forward
0 new messages