<?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 Fix hard returns/carriages in text fields using update cursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150532#M63942</link>
    <description>&lt;P&gt;Pretty new to cursors here - trying to write a script that will remove any hard 'returns'/newlines/carriages from any text values in a feature class.&lt;/P&gt;&lt;P&gt;For example, a user might enter:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TigerWoulds_0-1646361110171.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/35561i42EEBEF046CC9BF5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TigerWoulds_0-1646361110171.png" alt="TigerWoulds_0-1646361110171.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What I need is:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TigerWoulds_1-1646361143348.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/35562iE608A11A890BAAE0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TigerWoulds_1-1646361143348.png" alt="TigerWoulds_1-1646361143348.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Can someone clarify why I'm getting the error&amp;nbsp;TypeError: sequence size must match size of the row.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os
import time

# Start the clock
startTime = time.time()

# Input geodatabase with feature classes to update
inputGDB = r'C:\data\test.gdb'

# Set workspace to input GDB
arcpy.env.workspace = inputGDB

eolchar = ""

# Get a list of feature datasets
datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []

# Loop through all feature classes (including ones in feature datasets)
print('Reading Feature Classes...')
for ds in datasets:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        # Get the name of the feature class
        path = os.path.join(arcpy.env.workspace, fc)
        desc = arcpy.Describe(path)
        fcName = desc.name
        print('Cleaning feature class: {}'.format(fcName))

        # Build a list of all TEXT/String Fields
        fieldList = [i.name for i in arcpy.ListFields(fc) if i.type == 'String']
        fieldList = ['OID@'] + fieldList

        # Iterate through each row and execute the clean
        # row = [i.replace(chr(10), eolchar).replace(chr(13), eolchar) if i is not None else i for i in row]

        with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
            for row in cursor:
                for i in row:
                    if i is not None:
                        if chr(10) in str(i) or chr(13) in str(i):
                            print('\nHard Return found in feature class: {}'.format(fc))
                            print('Object ID: {}'.format(row[0]))
                            print('There is a newline in {}'.format(i))
                            # print ('Field is {}'.format(fieldList))
                            row = i.replace(chr(10), eolchar).replace(chr(13), eolchar)
                            cursor.updateRow(row)

        print('Finished cleaning.')

print('Completed.')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;If I use the following below, it works.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;# row = [i.replace(chr(10), eolchar).replace(chr(13), eolchar) if i is not None else i for i in row]&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;But I'm trying the print the Feature Class name, OID, and Field Name if/where any changes are made. Using the code above, how can I extract the field name where hard returns/carriages are removed?&lt;/P&gt;</description>
    <pubDate>Fri, 04 Mar 2022 02:50:21 GMT</pubDate>
    <dc:creator>tigerwoulds</dc:creator>
    <dc:date>2022-03-04T02:50:21Z</dc:date>
    <item>
      <title>Fix hard returns/carriages in text fields using update cursor</title>
      <link>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150532#M63942</link>
      <description>&lt;P&gt;Pretty new to cursors here - trying to write a script that will remove any hard 'returns'/newlines/carriages from any text values in a feature class.&lt;/P&gt;&lt;P&gt;For example, a user might enter:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TigerWoulds_0-1646361110171.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/35561i42EEBEF046CC9BF5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TigerWoulds_0-1646361110171.png" alt="TigerWoulds_0-1646361110171.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What I need is:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TigerWoulds_1-1646361143348.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/35562iE608A11A890BAAE0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TigerWoulds_1-1646361143348.png" alt="TigerWoulds_1-1646361143348.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Can someone clarify why I'm getting the error&amp;nbsp;TypeError: sequence size must match size of the row.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os
import time

# Start the clock
startTime = time.time()

# Input geodatabase with feature classes to update
inputGDB = r'C:\data\test.gdb'

# Set workspace to input GDB
arcpy.env.workspace = inputGDB

eolchar = ""

# Get a list of feature datasets
datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []

# Loop through all feature classes (including ones in feature datasets)
print('Reading Feature Classes...')
for ds in datasets:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        # Get the name of the feature class
        path = os.path.join(arcpy.env.workspace, fc)
        desc = arcpy.Describe(path)
        fcName = desc.name
        print('Cleaning feature class: {}'.format(fcName))

        # Build a list of all TEXT/String Fields
        fieldList = [i.name for i in arcpy.ListFields(fc) if i.type == 'String']
        fieldList = ['OID@'] + fieldList

        # Iterate through each row and execute the clean
        # row = [i.replace(chr(10), eolchar).replace(chr(13), eolchar) if i is not None else i for i in row]

        with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
            for row in cursor:
                for i in row:
                    if i is not None:
                        if chr(10) in str(i) or chr(13) in str(i):
                            print('\nHard Return found in feature class: {}'.format(fc))
                            print('Object ID: {}'.format(row[0]))
                            print('There is a newline in {}'.format(i))
                            # print ('Field is {}'.format(fieldList))
                            row = i.replace(chr(10), eolchar).replace(chr(13), eolchar)
                            cursor.updateRow(row)

        print('Finished cleaning.')

print('Completed.')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;If I use the following below, it works.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;# row = [i.replace(chr(10), eolchar).replace(chr(13), eolchar) if i is not None else i for i in row]&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;But I'm trying the print the Feature Class name, OID, and Field Name if/where any changes are made. Using the code above, how can I extract the field name where hard returns/carriages are removed?&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 02:50:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150532#M63942</guid>
      <dc:creator>tigerwoulds</dc:creator>
      <dc:date>2022-03-04T02:50:21Z</dc:date>
    </item>
    <item>
      <title>Re: Fix hard returns/carriages in text fields using update cursor</title>
      <link>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150606#M63946</link>
      <description>&lt;P&gt;Probably access the field using the iteration number as an index to fieldList, maybe better ways.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
  for row in cursor:
    for counter, i in enumerate(row):
      current_field = fieldList[counter]
                    &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 10:54:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150606#M63946</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2022-03-04T10:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: Fix hard returns/carriages in text fields using update cursor</title>
      <link>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150620#M63948</link>
      <description>&lt;P&gt;'&lt;SPAN&gt;TypeError: sequence size must match size of the row' typically means that the length of the field list you use to create the cursor does not match the length of the list you use when doing the insert/delete/update row.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 12:35:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150620#M63948</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2022-03-04T12:35:56Z</dc:date>
    </item>
    <item>
      <title>Re: Fix hard returns/carriages in text fields using update cursor</title>
      <link>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150660#M63950</link>
      <description>&lt;P&gt;Great, thanks. That's what I needed to get the field name. Still getting the sequence error though.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;with &lt;/SPAN&gt;arcpy&lt;SPAN&gt;.&lt;/SPAN&gt;da&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;UpdateCursor&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;fc&lt;SPAN&gt;, &lt;/SPAN&gt;fieldList&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN&gt;as &lt;/SPAN&gt;cursor&lt;SPAN&gt;:&lt;BR /&gt;&lt;/SPAN&gt;    &lt;SPAN&gt;for &lt;/SPAN&gt;row &lt;SPAN&gt;in &lt;/SPAN&gt;cursor&lt;SPAN&gt;:&lt;BR /&gt;&lt;/SPAN&gt;        objectID &lt;SPAN&gt;= &lt;/SPAN&gt;row&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;]&lt;BR /&gt;&lt;/SPAN&gt;        &lt;SPAN&gt;for &lt;/SPAN&gt;counter&lt;SPAN&gt;, &lt;/SPAN&gt;i &lt;SPAN&gt;in &lt;/SPAN&gt;&lt;SPAN&gt;enumerate&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;row&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;SPAN&gt;:&lt;BR /&gt;&lt;/SPAN&gt;            current_field &lt;SPAN&gt;= &lt;/SPAN&gt;fieldList&lt;SPAN&gt;[&lt;/SPAN&gt;counter&lt;SPAN&gt;]&lt;BR /&gt;&lt;/SPAN&gt;            &lt;SPAN&gt;if &lt;/SPAN&gt;i &lt;SPAN&gt;is not None and &lt;/SPAN&gt;&lt;SPAN&gt;chr&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;10&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN&gt;in &lt;/SPAN&gt;&lt;SPAN&gt;str&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;i&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN&gt;or &lt;/SPAN&gt;&lt;SPAN&gt;chr&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;13&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN&gt;in &lt;/SPAN&gt;&lt;SPAN&gt;str&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;i&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;SPAN&gt;:&lt;BR /&gt;&lt;/SPAN&gt;                    &lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'&lt;/SPAN&gt;&lt;SPAN&gt;\n&lt;/SPAN&gt;&lt;SPAN&gt;Error found on Object ID: {}'&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;format&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;objectID&lt;SPAN&gt;))&lt;BR /&gt;&lt;/SPAN&gt;                    &lt;SPAN&gt;print &lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'Field: {}'&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;format&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;current_field&lt;SPAN&gt;))&lt;BR /&gt;&lt;/SPAN&gt;                    &lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'There is a newline in: {}'&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;format&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;i&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;replace&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;chr&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;10&lt;/SPAN&gt;&lt;SPAN&gt;), &lt;/SPAN&gt;eolchar&lt;SPAN&gt;).&lt;/SPAN&gt;&lt;SPAN&gt;replace&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;chr&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;13&lt;/SPAN&gt;&lt;SPAN&gt;), &lt;/SPAN&gt;eolchar&lt;SPAN&gt;)))&lt;BR /&gt;&lt;/SPAN&gt;                    row &lt;SPAN&gt;= &lt;/SPAN&gt;&lt;SPAN&gt;[&lt;/SPAN&gt;i&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;replace&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;chr&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;10&lt;/SPAN&gt;&lt;SPAN&gt;), &lt;/SPAN&gt;eolchar&lt;SPAN&gt;).&lt;/SPAN&gt;&lt;SPAN&gt;replace&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;chr&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;13&lt;/SPAN&gt;&lt;SPAN&gt;), &lt;/SPAN&gt;eolchar&lt;SPAN&gt;)]&lt;BR /&gt;&lt;/SPAN&gt;                    cursor&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;updateRow&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;row&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Mar 2022 14:40:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150660#M63950</guid>
      <dc:creator>tigerwoulds</dc:creator>
      <dc:date>2022-03-04T14:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: Fix hard returns/carriages in text fields using update cursor</title>
      <link>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150671#M63951</link>
      <description>&lt;P&gt;I think if you dump out the length of 'row' after this statement if will be 1, which causes the error since it must be the length fieldList when you call updateRow&lt;/P&gt;&lt;LI-CODE lang="python"&gt;row = [i.replace(chr(10), eolchar).replace(chr(13), eolchar)]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 15:02:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150671#M63951</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2022-03-04T15:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: Fix hard returns/carriages in text fields using update cursor</title>
      <link>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150674#M63952</link>
      <description>&lt;P&gt;Taking David's enumerate example and changing the variables to match the index, value tuple it creates:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Get a list of feature datasets
datasets = [ds for ds in arcpy.ListDatasets(feature_type='feature') if ds is not None]

# Loop through all feature classes (including ones in feature datasets)
print('Reading Feature Classes...')
for ds in datasets:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        # Get the name of the feature class
        print('Cleaning feature class: {}'.format(fc))

        # Build a list of all TEXT/String Fields
        fieldList = [fld.name for fld in arcpy.ListFields(fc) if fld.type == 'String']
        fieldList = ['OID@'] + fieldList

        # Iterate through each row and execute the clean
        # row = [i.replace(chr(10), eolchar).replace(chr(13), eolchar) if i is not None else i for i in row]
        with arcpy.da.UpdateCursor(fc, fieldList) as cursor:
            for row in cursor:
                changed = False
                # i is the index, val is the row[index] value
                for i, val in enumerate(row):
                    # Skip OID field, nulls and ''
                    if all([i != 0, val not in [None, '']]): #&amp;lt;- you can add ' ' if you want to exclude those values. 
                        # Check if the value ends in one of these
                        if val[-1] in [' ', '\t', '\n', '\r']:
                            changed = True # &amp;lt;- set flag to indicate this row has changes and should be updated
                            print('\nHard Return found in feature class: {}'.format(fc))
                            print('Object ID: {}'.format(row[0]))
                            print('There is a newline in {} val: {}'.format(fieldList[i], val))
                            row[i] = val.strip(' \t\n\r')

                # Check if anything changed and needs to be updated.
                if changed:
                    cursor.updateRow(row)

        print('Finished cleaning.')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 15:12:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fix-hard-returns-carriages-in-text-fields-using/m-p/1150674#M63952</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-03-04T15:12:28Z</dc:date>
    </item>
  </channel>
</rss>

