<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Read dbf with arcpy in PyCharm? in Esri Labs Questions</title>
    <link>https://community.esri.com/t5/esri-labs-questions/read-dbf-with-arcpy-in-pycharm/m-p/1116646#M54</link>
    <description>&lt;P&gt;I have exported an&amp;nbsp;ArcGIS Desktop 10.7 table&amp;nbsp;into a&amp;nbsp;dbf&amp;nbsp;file.&lt;BR /&gt;Now I want to do some GIS calculation in standalone Python.&lt;BR /&gt;Therefore I have started a&amp;nbsp;PyCharm&amp;nbsp;project referencing the ArcGIS Python interpreter and hence am able to import&amp;nbsp;arcpy&amp;nbsp;into my&amp;nbsp;main.py.&lt;BR /&gt;Problem is: &lt;A href="https://www.jasonmolding.com" target="_self"&gt;I&lt;/A&gt; don't want to pip install other modules, but I don't know how to correctly read the dbf table with arcpy.&lt;/P&gt;&lt;PRE&gt;#encoding=utf-8 
import arcpy
path=r"D:\test.dbf"
sc=arcpy.SearchCursor(path) # Does not work: IOError exception str() failed
tv=arcpy.mapping.TableView(path) # Does not work either: StandaloneObject invalid data source or table&lt;/PRE&gt;&lt;P&gt;The dbf file is correct, it can be read into ArcGIS.&lt;BR /&gt;Can someone please give me an idea, how to read the file standalone with&amp;nbsp;arcpy?&lt;/P&gt;</description>
    <pubDate>Sun, 14 Nov 2021 06:46:41 GMT</pubDate>
    <dc:creator>AaqibJamshed</dc:creator>
    <dc:date>2021-11-14T06:46:41Z</dc:date>
    <item>
      <title>Read dbf with arcpy in PyCharm?</title>
      <link>https://community.esri.com/t5/esri-labs-questions/read-dbf-with-arcpy-in-pycharm/m-p/1116646#M54</link>
      <description>&lt;P&gt;I have exported an&amp;nbsp;ArcGIS Desktop 10.7 table&amp;nbsp;into a&amp;nbsp;dbf&amp;nbsp;file.&lt;BR /&gt;Now I want to do some GIS calculation in standalone Python.&lt;BR /&gt;Therefore I have started a&amp;nbsp;PyCharm&amp;nbsp;project referencing the ArcGIS Python interpreter and hence am able to import&amp;nbsp;arcpy&amp;nbsp;into my&amp;nbsp;main.py.&lt;BR /&gt;Problem is: &lt;A href="https://www.jasonmolding.com" target="_self"&gt;I&lt;/A&gt; don't want to pip install other modules, but I don't know how to correctly read the dbf table with arcpy.&lt;/P&gt;&lt;PRE&gt;#encoding=utf-8 
import arcpy
path=r"D:\test.dbf"
sc=arcpy.SearchCursor(path) # Does not work: IOError exception str() failed
tv=arcpy.mapping.TableView(path) # Does not work either: StandaloneObject invalid data source or table&lt;/PRE&gt;&lt;P&gt;The dbf file is correct, it can be read into ArcGIS.&lt;BR /&gt;Can someone please give me an idea, how to read the file standalone with&amp;nbsp;arcpy?&lt;/P&gt;</description>
      <pubDate>Sun, 14 Nov 2021 06:46:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/esri-labs-questions/read-dbf-with-arcpy-in-pycharm/m-p/1116646#M54</guid>
      <dc:creator>AaqibJamshed</dc:creator>
      <dc:date>2021-11-14T06:46:41Z</dc:date>
    </item>
    <item>
      <title>Re: Read dbf with arcpy in PyCharm?</title>
      <link>https://community.esri.com/t5/esri-labs-questions/read-dbf-with-arcpy-in-pycharm/m-p/1116652#M55</link>
      <description>&lt;PRE&gt;path=r"D:\test.dbf"&lt;/PRE&gt;&lt;P&gt;Is in the root directory, try putting it into a folder&lt;/P&gt;&lt;PRE&gt;path=r"D:\TestData\test.dbf"&lt;/PRE&gt;&lt;P&gt;Also, arcpy.da cursors&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/searchcursor-class.htm" target="_blank" rel="noopener"&gt;SearchCursor—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;And you need to specify the fields to use (field_names)&amp;nbsp; they are not optional.&amp;nbsp; you only specified the file, and not the fields, so it will fail&lt;/P&gt;&lt;PRE&gt;SearchCursor (in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause}, {datum_transformation})&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Nov 2021 10:24:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/esri-labs-questions/read-dbf-with-arcpy-in-pycharm/m-p/1116652#M55</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-11-14T10:24:59Z</dc:date>
    </item>
    <item>
      <title>Re: Read dbf with arcpy in PyCharm?</title>
      <link>https://community.esri.com/t5/esri-labs-questions/read-dbf-with-arcpy-in-pycharm/m-p/1136132#M56</link>
      <description>&lt;P&gt;Using Pandas&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;PRE&gt;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&lt;/PRE&gt;&lt;P&gt;Using CURSOR&lt;/P&gt;&lt;P&gt;You can also use arcpy cursors and&amp;nbsp;dict&amp;nbsp;for simple calculation.&lt;/P&gt;&lt;P&gt;There are simple example on this page on how to use correctly cursors :&amp;nbsp;&lt;A href="https://desktop.arcgis.com/fr/arcmap/10.3/analyze/arcpy-data-access/searchcursor-class.htm" target="_blank" rel="nofollow noopener noreferrer"&gt;https://desktop.arcgis.com/fr/arcmap/10.3/analyze/arcpy-data-access/searchcursor-class.htm&lt;/A&gt;&lt;A href="https://tinyurl.com/2p9ev2cu/" target="_self"&gt;l&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Jan 2022 09:55:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/esri-labs-questions/read-dbf-with-arcpy-in-pycharm/m-p/1136132#M56</guid>
      <dc:creator>Salenley</dc:creator>
      <dc:date>2022-01-22T09:55:17Z</dc:date>
    </item>
  </channel>
</rss>

