I have a string similar to this "A["B"]C" I need this string to look like "A[B]C". Do you know how to do it?
You can use re.sub for this.
import re
string = "[6->['A', 'B']; 7->['ABC']]"
string = re.sub("'","",string)
That will remove all ' characters from the string.
To get rid of " as well:
string = re.sub("['\"]","",string)
Related
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
I have a ";" delimited string. I need to remove an entry from it. I tried to use slice but that does get sliced string but not the original-modified string.
Here is an example:
var str1: string = 'TY66447;BH31496;PA99001;';
var str2 = str1.slice(16, 23);
console.log(str1);
console.log(str2);
It gives:
TY66447;BH31496;PA99001;
PA99001
But what I want to achieve is TY66447;BH31496;
I am not sure if I am using the correct string method. Please guide how to achieve.
I don't understand what do you want, but it seems that you want:
str1.slice(0, 16);
I have a string value with an apostrophe like this:
"I\\xE2\\x80\\x99m going now."
How can I get correct apostrophe value?
"I`m going now."
As you know, \xE2\x80\x99 is the a unicode character U+2019 RIGHT SINGLE QUOTATION MARK, but I have a string representation instead of byte...
Perhaps this is what you want:
utf8_apostrophe = b'\xe2\x80\x99'.decode("utf8")
str = "I"+utf8_apostrophe+"m going now"
Aside:
I ran into this when converting a single quotation mark, within a UTF-8-encoded tweet, into a normal single quote.
import re
original_tweet = 'I’m going now'
string_apostrophe = "'"
print re.sub(utf8_apostrophe, string_apostrophe, original_tweet)
which produces
I'm going now
I have an object being passed to the controller from a view and it contained double spaces in it that I need to remove. I have gone down the standard path and used the correct functionality to try and remove these double spaces but it doesn’t work. The string did also have a space at the from and then end that I managed to remove with .trim().
Below is what I used to try and Replace all the double spaces:
object = params.objectValue.trim()
object.replaceAll(" ", " ")
This did not work and still had the spaces so I tried this:
newObj = ""
object = params.objectValue.trim()
newObj = object
newObj.toString().replace(" ", " ")
here is an example of the string text:
"the person drove the car on 21/12/04 at 12:00"
This didn’t work either, has anyone got any idea as i dont really understand what i can do to remove these double spaces?
Thanks in advance
replaceAll() doesn't mutate the original string, it returns a new string with the replaced elements. You need to reassign params.objectValue to the result of replaceAll()
params.objectValue = params.objectValue.replaceAll(" ", " ").trim()
I wrote the following line:
string QuoteTest2 = "Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
This example went well, but what can I do in case I want to write a multi paragraph string including quotes?
The following example shows that puting '#' before the doesn't cut it..
string QuoteTest2 = #"Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
The string ends and the second quote and the over just gives me errors, what should I do?
Use double quotes to escape ""
e.g.
string QuoteTest2 = #"Benjamin Netnayahu,""BB"", said that: ""Israel will not fall""";