Replace empty attributes through 0

1787
9
Jump to solution
11-17-2014 12:50 AM
JohannesBierer
Occasional Contributor III

import arcpy 

import os 

 

in_data = r"R:\natura 2000\8847.04 MaP\7517-341_Horber_Neckarhaenge\Daten_Offenland\2014-11-12_PE_Maßnahmenentwurf\Reperatur\Reperatur.gdb\data\massnahmen_anm56_141117" 

 

field_list = [] 

numeric_fields = ["Double", "Integer", "SmallInteger"] 

 

fields = arcpy.ListFields(in_data) 

for field in fields: 

    # if (field.type in numeric_fields): 

    field_list.append(field.name) 

 

# convert the list of fields to a semicolor delimited string 

fields = ";".join(field_list)   

 

rows = arcpy.UpdateCursor(in_data, '', '', fields) 

count = len(field_list) 

 

for row in rows: 

    for k in range(count): 

        if row.isNull(field_list): 

            row.setValue(field_list, 0) 

        rows.updateRow(row)

This doesn't work? Why?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Johannes... In the academic world...I don't worry about those things

View solution in original post

0 Kudos
9 Replies
JohannesBierer
Occasional Contributor III

Fields to replace Null throught "0" are string fields

0 Kudos
DanPatterson_Retired
MVP Emeritus

not sure what the question is, but if it is a string field, and you want a zero representation then shouldn't nulls be replaced with "0" not 0

0 Kudos
JohannesBierer
Occasional Contributor III

We work here with a GIS System which doen't allow Null values, so I try to repair the shape. Like this it is not loadable in that other GIS System.

Tried "0", nothing happens.

Might it be that the path is not reachable for python, with the point inside?

... natura 2000\8847.04 MaP\7517-341_ ...

0 Kudos
JohannesBierer
Occasional Contributor III

This one, if you are interestesd in:

Welcome | Disy

0 Kudos
DanPatterson_Retired
MVP Emeritus

Thanks for the link...interesting company

0 Kudos
DanPatterson_Retired
MVP Emeritus

using the os module in Python here are the results with a truncated path/filename and yours

>>> p = r"R:\natura 2000\8847.04 MaP\7517-341_Horber_Neckarhaenge\Daten_Offenland\2014-11\test.py"

>>> import os

>>> path, file_name =  os.path.split(p)

>>> path

'R:\\natura 2000\\8847.04 MaP\\7517-341_Horber_Neckarhaenge\\Daten_Offenland\\2014-11'

>>> file_name

'test.py'

>>> p = r"R:\natura 2000\8847.04 MaP\7517-341_Horber_Neckarhaenge\Daten_Offenland\2014-11-12_PE_Maßnahmenentwurf\Reperatur\Reperatur.gdb\data\massnahmen_anm56_141117"

>>> p

'R:\\natura 2000\\8847.04 MaP\\7517-341_Horber_Neckarhaenge\\Daten_Offenland\\2014-11-12_PE_Ma\xc3\x9fnahmenentwurf\\Reperatur\\Reperatur.gdb\\data\\massnahmen_anm56_141117'

>>> path, file_name =  os.path.split(p)

>>> path

'R:\\natura 2000\\8847.04 MaP\\7517-341_Horber_Neckarhaenge\\Daten_Offenland\\2014-11-12_PE_Ma\xc3\x9fnahmenentwurf\\Reperatur\\Reperatur.gdb\\data'

>>> file_name

'massnahmen_anm56_141117'

>>>

so you can be the judge
(you might try moving the file to c:\ temp for testing

0 Kudos
JohannesBierer
Occasional Contributor III

Thanks for the script.

Sorry for bothering you. It was a problem with one null geometrie feature.

So I should keep in mind to check the geometrie first.

Disy is quite a good solution, if you can't afford to pay esri exorbitant license fees.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Johannes... In the academic world...I don't worry about those things

0 Kudos
JohannesBierer
Occasional Contributor III

But maybe you want to use a gis in which you can't load corrupt shape files 🙂