Ich habe eine ArcGIS Desktop 10.7 Tabelle in eine dbf-Datei exportiert.Jetzt möchte ich einige GIS-Berechnungen in eigenständigem Python durchführen.Daher habe ich ein PyCharm-Projekt gestartet, das auf den ArcGIS Python-Interpreter verweist, und kann somit arcpy in mein main.py importieren.Das Problem ist: Ich<\/A> möchte keine anderen Module per pip installieren, aber ich weiß nicht, wie man die dbf-Tabelle mit arcpy korrekt liest.<\/P>#encoding=utf-8 import arcpypath=r"D:\test.dbf"sc=arcpy.SearchCursor(path) # Funktioniert nicht: IOError-Ausnahme str() fehlgeschlagentv=arcpy.mapping.TableView(path) # Funktioniert auch nicht: StandaloneObject ungültige Datenquelle oder Tabelle<\/PRE>Die dbf-Datei ist korrekt, sie kann in ArcGIS gelesen werden.Kann mir bitte jemand einen Tipp geben, wie man die Datei eigenständig mit arcpy liest?<\/P>
#encoding=utf-8 import arcpypath=r"D:\test.dbf"sc=arcpy.SearchCursor(path) # Funktioniert nicht: IOError-Ausnahme str() fehlgeschlagentv=arcpy.mapping.TableView(path) # Funktioniert auch nicht: StandaloneObject ungültige Datenquelle oder Tabelle<\/PRE>Die dbf-Datei ist korrekt, sie kann in ArcGIS gelesen werden.Kann mir bitte jemand einen Tipp geben, wie man die Datei eigenständig mit arcpy liest?<\/P>
Using Pandas
Python from ArcMap comes with some modules. You can load the data into a pandas.DataFrame and work with this format. Pandas is well-documented and there is a lot of already asked question about it all over the web. It's also super easy to do groupby or table manipulations.
import pandas as pd import arcpy def read_arcpy_table(self, table, fields='*', null_value=None): """ Transform a table from ArcMap into a pandas.DataFrame object table : Path the table fields : Fields to load - '*' loads all fields null_value : choose a value to replace null values """ fields_type = {f.name: f.type for f in arcpy.ListFields(table)} if fields == '*': fields = fields_type.keys() fields = [f.name for f in arcpy.ListFields(table) if f.name in fields] fields = [f for f in fields if f in fields_type and fields_type[f] != 'Geometry'] # Remove Geometry field if FeatureClass to avoid bug # Transform in pd.Dataframe np_array = arcpy.da.FeatureClassToNumPyArray(in_table=table, field_names=fields, skip_nulls=False, null_value=null_value) df = self.DataFrame(np_array) return df # Add the function into the loaded pandas module pd.read_arcpy_table = types.MethodType(read_arcpy_table, pd) df = pd.read_arcpy_table(table='path_to_your_table') # Do whatever calculations need to be done
Using CURSOR
You can also use arcpy cursors and dict for simple calculation.
There are simple example on this page on how to use correctly cursors : https://desktop.arcgis.com/fr/arcmap/10.3/analyze/arcpy-data-access/searchcursor-class.html
path=r"D:\test.dbf"
Is in the root directory, try putting it into a folder
path=r"D:\TestData\test.dbf"
Also, arcpy.da cursors
SearchCursor—ArcGIS Pro | Documentation
And you need to specify the fields to use (field_names) they are not optional. you only specified the file, and not the fields, so it will fail
SearchCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause}, {datum_transformation})
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.