I have this reoccurring nightmare... (see 'ascii' codec can't encode character u'\u201c' ) I keep coming back to this script where I need to replace a new line ('\n') in a field. I have somehow gotten past the error in the referenced thread, but now I'm faced with
TypeError: sequence size must match size of the row
<SPAN class="keyword token">import</SPAN> arcpy
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'J:\WaterQuality\test_tables.gdb'</SPAN>
table <SPAN class="operator token">=</SPAN> <SPAN class="string token">'FieldParameters'</SPAN>
fieldNames <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>f<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">for</SPAN> f <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>table<SPAN class="punctuation token">,</SPAN><SPAN class="string token">''</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'String'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> fieldName <SPAN class="keyword token">in</SPAN> fieldNames<SPAN class="punctuation token">:</SPAN>
field <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>fieldName<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>table<SPAN class="punctuation token">,</SPAN>field<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> None<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">pass</SPAN>
<SPAN class="keyword token">elif</SPAN> <SPAN class="string token">'\n'</SPAN> <SPAN class="keyword token">in</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'\n'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'=>'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
Water has a green<SPAN class="operator token">/</SPAN>grey tint
Water has a green<SPAN class="operator token">/</SPAN>grey tint<SPAN class="operator token">=</SPAN><SPAN class="operator token">>=</SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Line 18 through line 20 are returned from the print statement on line 13. The actual value of the field is:
Water has a gren/grey tint\n\n
Line 21 is returned from the print statement on line 15: I have successfully replaced both \n characters with =>
However, it errors with:
Runtime error
Traceback (most recent call last):
File "<string>", line 23, in <module> (line 23 is shown as line 16 above)
TypeError: sequence size must match size of the row
This table has 11 string fields, the first 9 do not have any \n characters in any of them. I hit the 10th field and the first record it finds with a \n tosses the error.
This is being run in the Arcpy window of ArcMap 10.6.1. I've tossed this error before with an insert cursor (see: Brain cramp with an Insert Cursor from 9 months ago) and I was able to resolve it then. I just don't see what I'm doing wrong this time.
My goal is to loop through a list of tables and their respective string fields looking for \n characters and replace them accordingly. In a search cursor and find all the offenders just fine, but fixing them is another story.