I want to add some text within two delimiters in a string.
Previous string:
'ABC [123]'
New string needs to be like this:
'ABC [123 sometext]'
How do I do that?
slightly more versatile I'd say, without using replace:
s = 'ABC [123]'
insert = 'sometext'
insert_after = '123'
delimiter = ' '
ix = s.index(insert_after)
if ix != -1:
s = s[:ix+len(insert_after)] + delimiter + insert + s[ix+len(insert_after):]
# or with an f-string:
# s = f"{s[:ix+len(insert_after)]}{delimiter}{insert}{s[ix+len(insert_after):]}"
print(s)
# ABC [123 sometext]
If the insert patterns get more complex, I'd also suggest to take a look at regex. If the pattern is simple however, not using regex should be the more efficient solution.
Most of these types of changes depend on prerequisite knowledge of string pattern.
In your case simple str.replace would do the trick.
varstr = 'ABC [123]';
varstr.replace(']',' sometext]');
You can profit a lot from str doc and diving into regex;
All the above answers are correct but if somehow you are trying to add a variable
variable_string = 'ABC [123]'
sometext = "The text you want to add"
variable_string = variable_string.replace("]", " " + sometext + "]")
print(variable_string)
Related
I am trying to write a function to remove all punctuation characters from a string. I've tried several permutations on translate, replace, strip, etc. My latest attempt uses a brute force approach:
def clean_lower(sample):
punct = list(string.punctuation)
for c in punct:
sample.replace(c, ' ')
return sample.split()
That gets rid of almost all of the punctuation but I'm left with // in front of one of the words. I can't seem to find any way to remove it. I've even tried explicitly replacing it with sample.replace('//', ' ').
What do I need to do?
using translate is the fastest way to remove punctuations, this will remove // too:
import string
s = "This is! a string, with. punctuations? //"
def clean_lower(s):
return s.translate(str.maketrans('', '', string.punctuation))
s = clean_lower(s)
print(s)
Use regular expressions
import re
def clean_lower(s):
return(re.sub(r'\W','',s))
Above function erases any symbols except underscore
Perhaps you should approach it from the perspective of what you want to keep:
For example:
import string
toKeep = set(string.ascii_letters + string.digits + " ")
toRemove = set(string.printable) - toKeep
cleanUp = str.maketrans('', '', "".join(toRemove))
usage:
s = "Hello! world of / and dice".translate(cleanUp)
# s will be 'Hello world of and dice'
as suggested by #jasonharper you need to redefine "sample" and it should work:
import string
sample='// Hello?) // World!'
print(sample)
punct=list(string.punctuation)
for c in punct:
sample=sample.replace(c,'')
print(sample.split())
I have the following str I want to delete characters.
For example:
from str1 = "A.B.1912/2013(H-0)02322"
to 1912/2013
from srt2 = "I.M.1591/2017(I-299)17529"
to 1591/2017
from str3 = "I.M.C.15/2017(I-112)17529"
to 15/2017
I'm trying this way, but I need to remove the rest from ( to the right
newStr = str1.strip('A.B.')
'1912/2013(H-0)02322'
For the moment I'm doing it with slice notation
str1 = "A.B.1912/2013(H-0)02322"
str1 = str1[4:13]
'1912/2013'
But not all have the same length.
Any ideas or suggestions?
With some (modest) assumptions about the format of the strings, here's a solution without using regex:
First split the string on the ( character, keeping the substring on the left:
left = str1.split( '(' )[0] # "A.B.1912/2013"
Then, split the result on the last . (i.e. split from the right just once), keeping the second component:
cut = left.rsplit('.', 1)[1] # "1912/2013"
or combining the two steps into a function:
def extract(s):
return s.split('(')[0].rsplit('.', 1)[1]
Use a regex instead:
import re
regex = re.compile(r'\d+/\d+')
print(regex.search(str1).group())
print(regex.search(str2).group())
print(regex.search(str3).group())
Output:
1912/2013
1591/2017
15/2017
We can try using re.sub here with a capture group:
str1 = "A.B.1912/2013(H-0)02322"
output = re.sub(r'.*\b(\d+/\d+)\b.*', '\\1', str1)
print(output)
1912/2013
You have to use a regular expression to solve this problem.
import re
pattern = r'\d+/\d+'
str1 = "A.B.1912/2013(H-0)02322"
srt2 = "I.M.1591/2017(I-299)17529"
str3 = "I.M.C.15/2017(I-112)17529"
print(*re.findall(pattern, str1))
print(*re.findall(pattern, str2))
print(*re.findall(pattern, str3))
Output:
1912/2013
1591/2017
15/2017
I'm trying to do a search and replace (for multiple chars) in the following string:
VAR=%2FlkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA%2B7G3e8%3D&
One or more of these characters: %3D, %2F, %2B, %23, can be found anywhere (beginning, middle, or end of the string) and ideally, I'd like to search for all of them at once (using one regex) and replace them with = or / or + or # respectively, then return the final string.
Example 1:
VAR=%2FlkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA%2B7G3e8%3D&
Should return
VAR=/lkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA+7G3e8=&
Example 2:
VAR=s2P0n6I%2Flonpj6uCKvYn8PCjp%2F4PUE2TPsltCdmA%3DRQPY%3D&
Should return
VAR=s2P0n6I/lonpj6uCKvYn8PCjp/4PUE2TPsltCdmA=RQPY=&
I'm not convinced you need regex for this, but it's fairly easy to do with Python:
x = 'VAR=%2FlkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA%2B7G3e8%3D&'
import re
MAPPING = {
'%3D': '=',
'%2F': '/',
'%2B': '+',
'%23': '#',
}
def replace(match):
return MAPPING[match.group(0)]
print x
print re.sub('%[A-Z0-9]{2}', replace, x)
Output:
VAR=%2FlkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA%2B7G3e8%3D&
VAR=/lkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA+7G3e8=&
There is no need for a regex to do that in your example. A simple replace method will do:
def rep(s):
for pat, txt in [['%2F','/'], ['%2B','+'], ['%3D','='], ['%23','#']]:
s = s.replace(pat, txt)
return s
I'm also not convinced you need regex, but there's a better way to do url-decode with regex. Basically you need that every string in the pattern of %XX will be converted into the char it represents. This can be done with re.sub() like so:
>>> VAR="%2FlkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA%2B7G3e8%3D&"
>>> re.sub(r'%..', lambda x: chr(int(x.group()[1:], 16)), VAR)
'/lkdMu9zkpE8w7UKDOtkkHhJlYZ6CaEaxqmsA+7G3e8=&'
Enjoy.
var = "VAR=s2P0n6I%2Flonpj6uCKvYn8PCjp%2F4PUE2TPsltCdmA%3DRQPY%3D&"
var = var.replace("%2F", "/")
var = var.replace("%2B", "+")
var = var.replace("%3D", "=")
but you got same result with urllib2.unquote
import urllib2
var = "VAR=s2P0n6I%2Flonpj6uCKvYn8PCjp%2F4PUE2TPsltCdmA%3DRQPY%3D&"
var = urllib2.unquote(var)
This can't be done with a regex because there's no way to write any kind of conditional inside of a regex. Regular expressions can only answer the question "Does this string match this pattern?" and not perform the operation "If this string matches this pattern, replace part of it with this. If it matches this pattern, replace it with this. etc..."
I would like to replace (and not remove) all punctuation characters by " " in a string in Python.
Is there something efficient of the following flavour?
text = text.translate(string.maketrans("",""), string.punctuation)
This answer is for Python 2 and will only work for ASCII strings:
The string module contains two things that will help you: a list of punctuation characters and the "maketrans" function. Here is how you can use them:
import string
replace_punctuation = string.maketrans(string.punctuation, ' '*len(string.punctuation))
text = text.translate(replace_punctuation)
Modified solution from Best way to strip punctuation from a string in Python
import string
import re
regex = re.compile('[%s]' % re.escape(string.punctuation))
out = regex.sub(' ', "This is, fortunately. A Test! string")
# out = 'This is fortunately A Test string'
This workaround works in python 3:
import string
ex_str = 'SFDF-OIU .df !hello.dfasf sad - - d-f - sd'
#because len(string.punctuation) = 32
table = str.maketrans(string.punctuation,' '*32)
res = ex_str.translate(table)
# res = 'SFDF OIU df hello dfasf sad d f sd'
There is a more robust solution which relies on a regex exclusion rather than inclusion through an extensive list of punctuation characters.
import re
print(re.sub('[^\w\s]', '', 'This is, fortunately. A Test! string'))
#Output - 'This is fortunately A Test string'
The regex catches anything which is not an alpha-numeric or whitespace character
Replace by ''?.
What's the difference between translating all ; into '' and remove all ;?
Here is to remove all ;:
s = 'dsda;;dsd;sad'
table = string.maketrans('','')
string.translate(s, table, ';')
And you can do your replacement with translate.
In my specific way, I removed "+" and "&" from the punctuation list:
all_punctuations = string.punctuation
selected_punctuations = re.sub(r'(\&|\+)', "", all_punctuations)
print selected_punctuations
str = "he+llo* ithis& place% if you * here ##"
punctuation_regex = re.compile('[%s]' % re.escape(selected_punctuations))
punc_free = punctuation_regex.sub("", str)
print punc_free
Result: he+llo ithis& place if you here
I have an access table that has a bunch coordinate values in degrees minutes seconds and they are formatted like this:
90-12-28.15
I want to reformat it like this:
90° 12' 28.15"
essentially replacing the dashes with the degrees minutes and seconds characters and a space between the degrees and minutes and another one between the minutes and seconds.
I'm thinking about using the 'Replace' function, but I'm not sure how to replace the first instance of the dash with a degree character (°) and space and then detect the second instance of the dash and place the minute characters and a space and then finally adding the seconds character at the end.
Any help is appreciated.
Mike
While regular expressions and split() are fine solutions, doing this with replace() is rather easy.
lat = "90-12-28.15"
lat = lat.replace("-", "° ", 1)
lat = lat.replace("-", "' ", 1)
lat = lat + '"'
Or you can do it all on one line:
lat = lat.replace("-", "° ", 1).replace("-", "' ", 1) + '"'
I would just split your first string:
# -*- coding: utf-8 -*-
str = '90-12-28.15'
arr = str.split('-')
str2 = arr[0] +'° ' + arr[1] + '\'' +arr[2] +'"'
print str2
You might want to use Python's regular expressions module re, particularly re.sub(). Check the Python docs here for more information.
If you're not familiar with regular expressions, check out this tutorial here, also from the Python documentation.
import re
text = 'replace "-" in 90-12-28.15'
print(re.sub(r'(\d\d)-(\d\d)-(\d\d)\.(\d\d)', r'''\1° \2' \3.\4"''', text))
# use \d{1,2} instead of \d\d if single digits are allowed
The python "replace" string method should be easy to use. You can find the documentation here.
In your case, you can do something like this:
my_str = "90-12-28.15"
my_str = my_str.replace("-","°",1)# 1 means you are going to replace the first ocurrence only
my_str = my_str.replace("-","'",1)
my_str = my_str + "\""