<?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 Re: Copy certain tabel fields and exclude field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1071585#M61464</link>
    <description>&lt;P&gt;The ObjectID_1 field most commonly happens when you import a feature class that already has an ObjectID field into a geodatabase, which tries to create its own OID field but since the default name is already taken, it names it with _1.&lt;/P&gt;&lt;P&gt;Because of the mismatched field names, you would need to &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/mapping-fields.htm" target="_self"&gt;create a field mappings object&lt;/A&gt;, which is a bit annoying for just one off-named field. You could recreate the feature class with only the one default named OID field so Append would work without field mappings, or use the cursors as you were attempting to do.&lt;/P&gt;&lt;P&gt;Here's what I would do for cursors&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;dsc = arcpy.Describe(Source)
target_field_Names = [f.name for f in arcpy.ListFields(Source)]
source_field_Names = [
    f.name for f in arcpy.ListFields(Source)
    if f.type != "OID"
    and f.name in target_field_Names
]
with arcpy.da.SearchCursor(Source,fieldnames) as sCur:
   with arcpy.da.InsertCursor(Target,lstFields) as iCur:
      for row in sCur:
          iCur.insertRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And here's what I would do for using append with field mappings. I created a fuzzy_fieldmap() function that will create field mappings only with fields that exist in both the source and target tables (excluding ObjectID fields).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

def main():
    fieldmappings = fuzzy_fieldmap(sourceTable, targetTable)

    arcpy.Append_management(
        sourceTable,
        targetTable,
        "NO_TEST",
        fieldmappings
    )
    print(arcpy.GetMessages())


def fuzzy_fieldmap(input_table, target_table):
    input_fields = [
        f.name.upper() for f in arcpy.ListFields(input_table)
        if f.type != "OID" or f.name != "OBJECTID"
    ]

    target_fields = [
        f.name.upper() for f in arcpy.ListFields(target_table)
        if f.type != "OID" or f.name != "OBJECTID"
    ]
    # Main FieldMapings object to hold FieldMap objects
    fms = arcpy.FieldMappings()
    # dictionary for FieldMap objects
    fm_vars = {}

    for t_field in target_fields:
        if t_field in input_fields:
            # Create the FieldMap object
            fm_vars[t_field] = arcpy.FieldMap()
            # Add fields to FieldMap object
            # Add target field first so the output gets those field properties
            fm_vars[t_field].addInputField(target_table, t_field)
            fm_vars[t_field].addInputField(input_table, t_field)
            # Add the FieldMap objects to the FieldMappings object
            fms.addFieldMap(fm_vars[t_field])

    # Optional debugging section to print field mappings
    for out_field in fms.fields:
        print("{} ({}): {}".format(out_field.name, out_field.aliasName, out_field.type))

    return fms


if __name__ == '__main__':
    main()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 23 Jun 2021 17:15:12 GMT</pubDate>
    <dc:creator>BlakeTerhune</dc:creator>
    <dc:date>2021-06-23T17:15:12Z</dc:date>
    <item>
      <title>Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070622#M61421</link>
      <description>&lt;P&gt;I am trying to use InsertCurosr to transfer rows from one table to another. Yes I could just use append but I am trying to see if the InsertCurosr is faster but I am getting the error&lt;/P&gt;&lt;P&gt;"DescribeData: Method Field1AFieldName does not exist".&lt;/P&gt;&lt;P&gt;Here is what I have currently. The Source Label currently does not have Field1A, Target does have the Field1A field.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy,os
#### Transfer Source table rows into Target Table

arcpy.env.workspace = r'C:\Temp\Test.gdb'

Target = r"C:\Temp\Test.gdb\TargetTable"
Source = r"C:\Temp\Test.gdb\SourceTable"
arcpy.DeleteRows_management(Target)

dsc = arcpy.Describe(Target)
fields = dsc.fields
out_fields = [dsc.OIDFieldName, dsc.Field1AFieldName]
fieldnames = [field.name for field in fields if field.name not in out_fields]

with arcpy.da.SearchCursor(Source,fieldnames) as sCur:
   with arcpy.da.InsertCursor(Target,fieldnames) as iCur:
      for row in sCur:
          iCur.insertRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 18:42:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070622#M61421</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-06-21T18:42:09Z</dc:date>
    </item>
    <item>
      <title>Re: Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070673#M61422</link>
      <description>&lt;P&gt;The issue (I guess) is that you're trying to access a property of the describe table object that just doesn't exist.&lt;/P&gt;&lt;P&gt;dsc.Field1AFieldName&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/table-properties.htm" target="_blank" rel="noopener"&gt;Table properties—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 20:00:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070673#M61422</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-06-21T20:00:09Z</dc:date>
    </item>
    <item>
      <title>Re: Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070702#M61425</link>
      <description>&lt;P&gt;The field Field1A doesn't exist in the source table only in the target. I guess I was thinking that line 12&amp;amp; 13 would take care of that issue for both the Source SearchCuror and the Target InsertCursor.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 20:33:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070702#M61425</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-06-21T20:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070714#M61428</link>
      <description>&lt;P&gt;you've created a describe object of the Target table then are trying to access a property of that describe object dsc.&lt;SPAN&gt;Field1AFieldName which doesn't exist and is causing the error.&amp;nbsp; Line 12 imho is the issue.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 21:01:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070714#M61428</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-06-21T21:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070717#M61429</link>
      <description>&lt;P&gt;I concur with&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;. You're trying to reference fields as if there were a join, but there isn't one.&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/add-join.htm" target="_blank"&gt;Add Join (Data Management)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 21:03:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070717#M61429</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-06-21T21:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070758#M61430</link>
      <description>&lt;P&gt;So can Field1A be skipped/not included when you pass the InsertCurosr, if so how?&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 22:50:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070758#M61430</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-06-21T22:50:06Z</dc:date>
    </item>
    <item>
      <title>Re: Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070763#M61431</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/102454"&gt;@2Quiker&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;So can Field1A be skipped/not included when you pass the InsertCurosr, if so how?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;Sure, just leave that field out of the&amp;nbsp;field_names parameter when you open the cursor. You only need to pass back values for the fields that you opened the cursor with.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jun 2021 22:53:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070763#M61431</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-06-21T22:53:06Z</dc:date>
    </item>
    <item>
      <title>Re: Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070997#M61439</link>
      <description>&lt;P&gt;I guess I was completely over thinking it.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;dsc = arcpy.Describe(Source)
fields = dsc.fields
#out_fields = [dsc.OIDFieldName]
fieldnames = [field.name for field in fields]

lstFields = [field.name for field in fields]

with arcpy.da.SearchCursor(Source,fieldnames) as sCur:
   with arcpy.da.InsertCursor(Target,lstFields) as iCur:
      for row in sCur:
          iCur.insertRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jun 2021 15:15:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1070997#M61439</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-06-22T15:15:05Z</dc:date>
    </item>
    <item>
      <title>Re: Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1071151#M61447</link>
      <description>&lt;P&gt;There's still some redundancy in your code. You can build a list of field names easily this way:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fieldnames = [f.name for f in arcpy.ListFields(Source)]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;But this is getting all the field names. If you want to exclude something, you could do something like:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;ignore_fields = ["Field1A", "SomethingElse"]
fieldnames = [f.name for f in arcpy.ListFields(Source) if f.name not in ignore_fields]&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;As a final note, it looks like you're just straight copying data; might I recommend &lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/append.htm" target="_self"&gt;Append()&lt;/A&gt;?&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jun 2021 19:06:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1071151#M61447</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-06-22T19:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1071568#M61463</link>
      <description>&lt;P&gt;Thank you for the replies, after looking at the data a little I was wrong, I was working with the wrong data sorry about that.&lt;/P&gt;&lt;P&gt;So the Source table has OBJECTID_1(not sure way but this is how I get the data) and the target has OBJECTID. So the InsurtCur 'Cannot find field 'OBJECTID_1'. The Source OBJECTID_1 should go into the targets OBJECTID but as I said it can't find it. Maybe I am still over thinking it...&lt;/P&gt;&lt;P&gt;If I use append&amp;nbsp; would the ObjectID vs ObjectID_1 be an issue?&lt;/P&gt;&lt;P&gt;I was trying use understand&amp;nbsp; search and insert cursors to understand them better.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jun 2021 16:29:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1071568#M61463</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-06-23T16:29:59Z</dc:date>
    </item>
    <item>
      <title>Re: Copy certain tabel fields and exclude field</title>
      <link>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1071585#M61464</link>
      <description>&lt;P&gt;The ObjectID_1 field most commonly happens when you import a feature class that already has an ObjectID field into a geodatabase, which tries to create its own OID field but since the default name is already taken, it names it with _1.&lt;/P&gt;&lt;P&gt;Because of the mismatched field names, you would need to &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/mapping-fields.htm" target="_self"&gt;create a field mappings object&lt;/A&gt;, which is a bit annoying for just one off-named field. You could recreate the feature class with only the one default named OID field so Append would work without field mappings, or use the cursors as you were attempting to do.&lt;/P&gt;&lt;P&gt;Here's what I would do for cursors&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;dsc = arcpy.Describe(Source)
target_field_Names = [f.name for f in arcpy.ListFields(Source)]
source_field_Names = [
    f.name for f in arcpy.ListFields(Source)
    if f.type != "OID"
    and f.name in target_field_Names
]
with arcpy.da.SearchCursor(Source,fieldnames) as sCur:
   with arcpy.da.InsertCursor(Target,lstFields) as iCur:
      for row in sCur:
          iCur.insertRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And here's what I would do for using append with field mappings. I created a fuzzy_fieldmap() function that will create field mappings only with fields that exist in both the source and target tables (excluding ObjectID fields).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

def main():
    fieldmappings = fuzzy_fieldmap(sourceTable, targetTable)

    arcpy.Append_management(
        sourceTable,
        targetTable,
        "NO_TEST",
        fieldmappings
    )
    print(arcpy.GetMessages())


def fuzzy_fieldmap(input_table, target_table):
    input_fields = [
        f.name.upper() for f in arcpy.ListFields(input_table)
        if f.type != "OID" or f.name != "OBJECTID"
    ]

    target_fields = [
        f.name.upper() for f in arcpy.ListFields(target_table)
        if f.type != "OID" or f.name != "OBJECTID"
    ]
    # Main FieldMapings object to hold FieldMap objects
    fms = arcpy.FieldMappings()
    # dictionary for FieldMap objects
    fm_vars = {}

    for t_field in target_fields:
        if t_field in input_fields:
            # Create the FieldMap object
            fm_vars[t_field] = arcpy.FieldMap()
            # Add fields to FieldMap object
            # Add target field first so the output gets those field properties
            fm_vars[t_field].addInputField(target_table, t_field)
            fm_vars[t_field].addInputField(input_table, t_field)
            # Add the FieldMap objects to the FieldMappings object
            fms.addFieldMap(fm_vars[t_field])

    # Optional debugging section to print field mappings
    for out_field in fms.fields:
        print("{} ({}): {}".format(out_field.name, out_field.aliasName, out_field.type))

    return fms


if __name__ == '__main__':
    main()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jun 2021 17:15:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/copy-certain-tabel-fields-and-exclude-field/m-p/1071585#M61464</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-06-23T17:15:12Z</dc:date>
    </item>
  </channel>
</rss>

