Select to view content in your preferred language

Find and delete Carriage returns

3882
6
Jump to solution
11-11-2021 03:29 AM
IFI
by
Occasional Contributor

Hi,

I am working with a Feature Service created from a Survey123 form.

Some of the users have inadvertently added carriage returns in their answers.

Is there a way to find the carriage returns and delete them, either using Find and Replace or a Field Calculator expression?

Thanks,

Paul 

 

0 Kudos
1 Solution

Accepted Solutions
MichaelVolz
Esteemed Contributor

Aldo:

Did you run this code in the field calculator or a standalone python script?

View solution in original post

6 Replies
Robert_LeClair
Esri Notable Contributor

There's a related thread here that adds a constraint to prevent survey responses from having trailing "whitespace" or "carriage returns" that seems to also prevent this from occurring in the middle of the response as well.  To remove extra spaces on existing attribute values using Arcade, there's a related thread here.  Good luck!

IFI
by
Occasional Contributor

Hi Robert,

Thanks for that! I still need to figure out a way of removing the carriage returns in bulk!

Thanks,

Paul

 

 

 

JoshuaBixby
MVP Esteemed Contributor

It isn't pretty, but it is simple to implement.  You can use a chained Python string replace call:

>>> s = "NRSP21_Rose Court_A\r\n_1_211012_162927447_16"
>>> print(s)
NRSP21_Rose Court_A
_1_211012_162927447_16
>>>
>>> s_new = s.replace("\r", "").replace("\n", "")
>>> print(s_new)
NRSP21_Rose Court_A_1_211012_162927447_16
>>>
IFI
by
Occasional Contributor

Hi Joshua,

Thanks for the reply. I guess I could implement that in a Field Calculator Expression?

Also, this replaces the carriage return (and space if I'm reading it correctly) for that particular row only, is that right? I would need a more complicated expression to find and replace them in a number of rows?

Thanks,

Paul

 

0 Kudos
Aldo_
by
New Contributor

Not sure if OP has already found a solution for multiple rows, but I had the same issue. Difference is I am dealing with a feature class - not hosted feature layer - with over 311,000 rows. Below is what I did in Pro to remove the carriage return and new line in one field of a feature class.

Note that I had to deal with thousands of nulls, which my first if statement identifies them as either None, no space, and single space, and then does nothing to it.

Then, instead of replacing "\r" and "\n", I have to use its respective chr() method, following @JoshuaBixby's replace function (thanks Joshua!)

Run time was about 2 minutes.

with arcpy.da.UpdateCursor("layerName","fieldName") as cursor:
        for row in cursor:
            if row[0] in (None,""," ","Anything-Else-To-Avoid"):
            else:
                row[0] = row[0].replace(chr(13),"").replace(chr(10),"")
                #if you wanted visual confirm use print(row[0])
                cursor.updateRow(row)

 

MichaelVolz
Esteemed Contributor

Aldo:

Did you run this code in the field calculator or a standalone python script?