Replacement of special characters in a string

Discuss RoboTask here
Post Reply
Lana_K
Posts: 31
Joined: Tue Jul 14, 2020 7:43 pm
Location: Bristol, UK

Replacement of special characters in a string

Post by Lana_K »

Hi,
I am wondering if someone could suggest which functions to use to replace various accented characters in a string (à, é, ó, č, ñ, etc). The task is to read a string from a CSV file and replace all non-Latin characters listed (no need to work with all existing characters, just the ones that the user encounters in her work, so this is a finite list of few tens of characters.
I created a task which replaces characters one by one using String Replace. Unfortunately, there is a problem with encoding and String Replace does not recognise the characters. And I found no way to control encoding there. I tried using Chr() and ASCII code of the character but usually such characters are encoded using two numbers and this method does not work. Pasting the character directly does not work either because by the time the string is passed to the String Replace function the encoding changes and the characters in the string are replaced with question marks, copyright symbols and so on.
Any suggestions?
Thank you.
Lana
Lana_K
Posts: 31
Joined: Tue Jul 14, 2020 7:43 pm
Location: Bristol, UK

Re: Replacement of special characters in a string

Post by Lana_K »

Just want to add an example.

This is how the original string looks like in the CSV file (not sure if this website would display the characters correctly when I post this, but I'll try anyway):

"Andrea Muñoz-Gutiérrez, Alma X. González-Morales, L. Á. García, V. Iršič, N. G. Karaçaylı, I. Pérez-Ràfols, C. Ramírez-Pérez, E. Gaztañaga, G. Tarlé"

And this is how the string looks like when passed to String Replace function:

"Andrea MuA±oz-GutiA©rrez, Alma X. GonzA?lez-Morales, L. A?. GarcA­a, V. IrA?iA?, N. G. KaraA§aylA±, I. PA©rez-RA fols, C. RamA­rez-PA©rez, E. GaztaA±aga, G. TarlA©"
Oleg
Site Admin
Posts: 3014
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Replacement of special characters in a string

Post by Oleg »

Str replace action works correctly
Download my example by the link: replacestr.zip
Unpack the task and import into RoboTask (Copy-Paste does not help)

Probably you read the source file incorrectly (without accounting for coding)
Could you send the source file to me (just several lines)? Pack it to ZIP-file and send to me by email to oleg@robotask.com
I try to create correct example for you
Oleg Yershov
Lana_K
Posts: 31
Joined: Tue Jul 14, 2020 7:43 pm
Location: Bristol, UK

Re: Replacement of special characters in a string

Post by Lana_K »

Thank you for the task. Mine looks very similar, only yours works and mine does not. I am yet to compare the two to see where I made a mistake.
I've emailed you a real life example file which I need to process.
Many thanks for your kind help.

Sincerely,
Lana
Oleg
Site Admin
Posts: 3014
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Replacement of special characters in a string

Post by Oleg »

Your file does not contain BOM (signature of format). So RoboTask reads this file as simple ACSII file. But the file has UTF-8 coding.
You should read this file as UTF-8 file
Look the demo task below to see difference

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|1040932817
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1497"
Hide=INTEGER|0
ID=INTEGER|-994657668
LogOnAsUser=INTEGER|1
Name=STRING|"demo - Read file with coding and without"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""filename"" with value ""C:\temp\jcap.csv"""
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"filename"
expand=STRING|"0"
linecount=STRING|"1"
varname=STRING|"filename"
varvalue=STRING|"C:\temp\jcap.csv"

[Actions\Action2]
ActionID=STRING|"A_DIALOG_SHOWTEXT"
Enabled=INTEGER|-1
Name=STRING|"Show Text //without coding"
Params=FOLDER

[Actions\Action2\Params]
autoclose=STRING|"0"
fromfile=STRING|"1"
line00000000=STRING|"{TextFile({filename})}"
linecount=STRING|"1"
timeout=STRING|"10"
title=STRING|"Text without coding"

[Actions\Action3]
ActionID=STRING|"A_DIALOG_SHOWTEXT"
Enabled=INTEGER|-1
Name=STRING|"Show Text //read file as UTF-8"
Params=FOLDER

[Actions\Action3\Params]
autoclose=STRING|"0"
fromfile=STRING|"1"
line00000000=STRING|"{TextFileUTF8({filename})}"
linecount=STRING|"1"
timeout=STRING|"10"
title=STRING|"Text as UTF-8"
Download my example how to read from file and replace necessary symbols: replace_str_from_file.zip
Unpack this task and import the task from file.
Oleg Yershov
Oleg
Site Admin
Posts: 3014
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Replacement of special characters in a string

Post by Oleg »

Read about BOM (Byte order mark) in wikipedia: https://en.wikipedia.org/wiki/Byte_order_mark
Oleg Yershov
Lana_K
Posts: 31
Joined: Tue Jul 14, 2020 7:43 pm
Location: Bristol, UK

Re: Replacement of special characters in a string

Post by Lana_K »

Many thanks. You are right, that file did not have BOM, but some others oddly did, even though I obtained them in the same way. Another mistake I made was trying to read the file as a CSV rather that TXT. I'll note this for future.

Thank you for helping.
Lana_K
Posts: 31
Joined: Tue Jul 14, 2020 7:43 pm
Location: Bristol, UK

Re: Replacement of special characters in a string

Post by Lana_K »

Dear Oleg,
I want to thank you again for your help with this special characters conversion problem. The task you kindly provided I used as a function called by my main task from a loop while passing the filename as a parameter on each iteration, and it worked perfectly on all files.

Totally agree with other users - Robotask is the best. It keeps me sane when I get boring repetitive housekeeping and data analysis tasks from my manager and colleagues.

Sincerely,
Lana
Post Reply