Replace a specific value in a string in python - python-3.x

I'm trying to replace a specific values in a long string. Is it possible to do this with replace function in python?
a snipit of the string is:
'rh':0, 'rp':0, 't':'b.nan','rh':1, 'rp':1, 't':'b.nan'
my snipit string should look like
'rh':0, 'rp':0, 't':b.nan,'rh':1, 'rp':1, 't':b.nan
i'm trying to replace the 'b.nan' to b.nan but it doesn't work.
The code i'm using:
a.replace("'b.nan'", "b.nan")

You can index strings like arrays:
string = "hello"
print(string[1])
this prints 'e'
You could try finding the index and then replacing it as such

Related

Extract value from string and get its content

I'm using LUA to extract a value from a long string.
String (shorted) looks like:
local input = "...</anno:rotation<anno:contentY>40.900000000000006</anno:contentY<..."
I'm looking for the quickest / easiest way to extract the value between <anno:contentY> and </anno:contentY<. In this example '40.900000000000006'.
Is there any better way then using string.find to get start and end position and then using string.split?

Do we have any methods in mel to check string contained in some another String or not

Do we have any methods in mel to check string contained in some another String or not.
For Example:
I had String like "mel".
I had Another String like "melcode".
Do we have any idea how to check the String "mel" is available in String "melcode".
num indexOf(str inputString1, str inputString2)
if the return value is -1 then inputString1 does not contain inputString2
As mentioned in Is there any possible way to filter movelets using mel from another movlet?
Hi Jaya Sankar, you can use indexOf which returns -1 if the first input String does not contain the second input String. There are also startsWith and endsWith as methods.

Convert a string to array in velocity

I have a velocity variable, like this:
$cur_record.getFieldValue("SelectRoles", $locale)
that is supposed to be an array. If I print its value, (just by putting $cur_record.getFieldValue("SelectRoles", $locale) in the code) i get:
["Accountant","Cashier"]
now, i want to iterate those 2 values, Accountant and Cashier, but it seems to be a String, not an Array, how can i convert that to an array so I can iterate it?..
I have tried to iterate it, but does not work, like this:
#foreach($bla_role in $cur_record.getFieldValue("SelectRoles", $locale))
$bla_role
#end
Also tried to get the value, as if it were an array, does not work either:
$cur_record.getFieldValue("SelectRoles", $locale).get(0)
I've tried setting it to another variable, like this:
#set($roleval = $cur_record.getFieldValue("SelectRoles", $locale))
$roleval.get(0)
but it does not work, but if i set a string, as the value is printed (the value hard coded), it does work!, like this:
#set($roleval = ["Accountant","Cashier"])
$roleval.get(0)
I dont know if I have to escape something, or I am missing something, can some one help me?
thank you!
You're trying to parse a String array that was previously serialized to String.
The following snippet uses substring, split and replace String methods to parse it.
#set($roleval = '["Accountant","Cashier"]')
#set($rolevalLengthMinusOne = $roleval.length() - 1)
#set($roles = $roleval.substring(1, $rolevalLengthMinusOne).split(","))
#foreach($role in $roles)
<h1>$role.replace('"',"")</h1>
#end
At first I tried to use #evaluate to parse it, but I ended up with these String methods.

rstrip() has no effect on string

Trying to use rstrip() at its most basic level, but it does not seem to have any effect at all.
For example:
string1='text&moretext'
string2=string1.rstrip('&')
print(string2)
Desired Result:
text
Actual Result:
text&moretext
Using Python 3, PyScripter
What am I missing?
someString.rstrip(c) removes all occurences of c at the end of the string. Thus, for example
'text&&&&'.rstrip('&') = 'text'
Perhaps you want
'&'.join(string1.split('&')[:-1])
This splits the string on the delimiter "&" into a list of strings, removes the last one, and joins them again, using the delimiter "&". Thus, for example
'&'.join('Hello&World'.split('&')[:-1]) = 'Hello'
'&'.join('Hello&Python&World'.split('&')[:-1]) = 'Hello&Python'

Lua: How to get string captures containing a specific substring?

In Lua, in want to get captures from a string containing a specific substring. E.g. in the string
test = "<item>foo</item> <item>bar</item>"
I want to get items containing "a", which in this case would be "bar". I tried this:
print(string.find(test, "<item>(.-a.-)</item>"))
but the result is:
1 34 foo</item> <item>bar
So .- is more greedy than I expected. What would be the correct pattern?
Try print(string.find(test, "<item>([^<]-a.-)</item>")).

Resources