Python script for replacing text with multiple characters

Hi,

Question,

There is list A, containing text.
There is List B, with Letter combinations.

IF there is a text combination present in list A which is also present in list B I would like to replace the the text in A with the corresponding index B.

So for example;
FD → 0
FC → 1
C → 6
FA → 3
G → 1
But all other text I would like to keep.

So in fact the same thing as the replace text command does, but than for the entire list.

I did find a python sript, which does the trick for ‘single Letters’ see below. However I also neet it for Letter combinations. Maybe by adding some brckets? (unfortunately I am not a python specialist).


2020-09-27 Find text item and index v2.gh (9.6 KB)

image

Any suggestions?

Thank you in advance,

Your regexp pattern is incorrect. It should be r'[A-Z]+'.

Your current pattern matches only one character, mine matches one-or-more. Do read up on https://docs.python.org/2/library/re.html#regular-expression-syntax :slight_smile:

3 Likes

Perfect!

Thank you! :+1: :+1:

Sorry 1 more specific thing.

Would it also be Possible to requognize text fragments including a '. so e.g.

List A
A’,B
BB to A

List B
A = 0
A’ = 1
B = 2
BB =3

Wanted result;
1,2
3 to 0

Thanks again! :slight_smile:

From the regexp page I linked you to you may have learned that [ and ] define a set of characters. You have currently [A-Z] as character set. To include ' in the set you…

guess…

correct. Add ' to the set: [A-Z'].

Another Python specific ReGexp howto: https://docs.python.org/2/howto/regex.html#regex-howto . Read it through met aandacht. A good tool to use while creating your regexp patterns with tests is https://regex101.com

1 Like

"[A-Z']+" and must use " instead of '

re.sub(r"[A-Z']+", lambda m: replacements[m.group()], t)

2 Likes

Hello the solution is not mine
You must choose the post of @nathanletwory

Hi Thank again, I am probably doing something wrong, but I still cant find it :sweat_smile:

image

2020-09-27 Find text item and index v3.gh (7.5 KB)

Any idea?
Thanks

You need to read also reply Python script for replacing text with multiple characters by @anon39580149

Thank again, I did, but strangly it returns the wrong numer…
It should be 3

image

any idea?
2020-09-27 Find text item and index v4.gh (8.1 KB)

In the file you attached the To translate component contains only three 4's, so the outcome is as expected. It’ll work when you se correct data…

Thank, but 4 is the ‘third’ item.
This means 4 would have to be replaced by a 3. (present in the replace item).

any idea?

Ehm… 4 isn’t a character in the set [A-Z']

1 Like

ahh offcourse :sweat_smile:! thats it thanks!
with addig 0-9 it works.

Thanks for your help!

:+1: :+1:

yay (:

Hi,

Sorry, but I encountered another challenge;

For instance I would like to change:
$1 --> 8
$2 --> 12

I got an error, also after I have added the $ into the python script. Any idea how I would have to add the python script?

Thanks :slight_smile:

2020-10-07 replace $1 Text.gh (9.7 KB)


image

Do revisit the document I linked to in my first reply. $ is a special character. Check on what \ means in regexp.

Hi, Thank again! :slight_smile:

I read the page an see indeed that $ is special character with a specific meaning.
Unfortunately I do not have any experience with python, so its really hard to find what I really want.

Lets zoom out a bit and see if you see an opportunity :slight_smile:

Panel A:
Is a panel containing text.
I would like to be able to modify some text parts of List.

Because of this I would like to introduce a ‘Variable character’ which represents a values (which I can change easily)

How the ‘Variable character’ looks like does not matter to me.

Original I suggested to use
$1
$2

but this could also be another character (e.g. €1 _1 '1)before a number. Simply something which makes the character ‘unique’

Thereafter I would like to replace the text with a certain value which is present in another list.
so e.g.
Replace $1 --> 7
Replace $2 --> 9

Do you have any suggestion what character/python adjustment would work?

Thank you very much.

2020-10-07 replace $1 Text_re.gh (10.0 KB)

In fact it the same as the ‘replace text commando does in GH’, unfortunately when there are a lote of ‘Variables’ this would look very ugly…
So I think should be done with python in a smarter way :).

This is not about Python but about regular expressions.

To use $ in a pattern as itself and not the special meaning you need to escape it. That is why I directed you to read up on the character \ as well, a bit further down after the text on $. You should’ve realized that you are supposed to use \$ in a pattern when you want to match the character $.

Use Wombat replace text (multiple)