I am trying to split R15.49 to (R, 15.49) or
ZAR15.49 to (ZAR, 15.49)
I have tried one of the solutions here and implememted the function below:
def splitCurrency(string):
match = re.search(r'([\D]+)([\d,]+)', string)
output = (match.group(1), match.group(2).replace(',',''))
return output
But I am getting (R, 15) or (ZAR, 15). and its ignoring the digits after the decimal place
If you want to fish out these values from a larger text, then use re.findall:
inp = "R15.49 or ZAR15.49"
matches = re.findall(r'\b([A-Z]+)(\d+(?:\.\d+)?)\b', inp)
print(matches)
This prints:
[('R', '15.49'), ('ZAR', '15.49')]
Related
I want to tokenize string up to 3rd occurence of some delimiter and then return the rest of the string as last element of the tokenize array.
Example:
I have a String which looks like this:
String someString= 1.22.33.4
Now im tokenizing it by delimiter '.' like this:
def (a, b, c, d) = someString.tokenize('.')
And it works, but only if number of dots are exactly 3.
Now if someone puts more number of dots like:
String someString = 1.22.33.4.55
Then it wouldn't work, because the number of variables won't match. So i want to make sure it only tokenizes up to 3rd dot, and then gives back whatever is left. So what i want to achieve in this case would be:
a = 1, b=22, c=33, d=4.55
How to do that?
You can use the version of split with the second argument to restrict
the returned items. E.g.
def (a,b,c,d) = '1.22.33.4.55'.split("\\.", 4)
assert ["1","22","33","4.55"] == [a,b,c,d]
Not a one liner but it works:
String someString= '1.22.33.4.55'
def stringArray = someString.tokenize('.')
def (a,b,c) = stringArray
def d = stringArray.drop(3).join('.')
println "a=$a, b=$b, c=$c, d=$d"
result:
a=1, b=22, c=33, d=4.55
string = 'jackk982'
y = 0
def is_alpha(x):
global y
for char in x:
for num in range(97, 123):
if ord(char) == num:
y += 1
x = x[y:]
return x
print(is_alpha(string))
I cant seem to find anything wrong, but the output does not give me 982. How can I fix this?
Okay, I don't know what you were trying to do with the two for-loops but the simplest way would be the following:
string = 'jackk982'
def get_string(x):
for a in range(0, len(x)):
if x[a].isdigit():
return x[a:]
print(get_string(string))
For the string jackk982, it returns 982. For another input hahahahlol5hf it returns 5hf.
The code first checks if one of the characters in the string is a number and if so it returns the rest of the string from that character including the number.
I am trying to convert a string of varchar to ascii. Then i'm trying to make it so any number that's not 3 digits has a 0 in front of it. then i'm trying to add a 1 to the very beginning of the string and then i'm trying to make it a large number that I can apply math to it.
I've tried a lot of different coding techniques. The closest I've gotten is below:
s = 'Ak'
for c in s:
mgk = (''.join(str(ord(c)) for c in s))
num = [mgk]
var = 1
num.insert(0, var)
mgc = lambda num: int(''.join(str(i) for i in num))
num = mgc(num)
print(num)
With this code I get the output: 165107
It's almost doing exactly what I need to do but it's taking out the 0 from the ord(A) which is 65. I want it to be 165. everything else seems to be working great. I'm using '%03d'% to insert the 0.
How I want it to work is:
Get the ord() value from a string of numbers and letters.
if the ord() value is less than 100 (ex: A = 65, add a 0 to make it a 3 digit number)
take the ord() values and combine them into 1 number. 0 needs to stay in from of 65. then add a one to the list. so basically the output will look like:
1065107
I want to make sure I can take that number and apply math to it.
I have this code too:
s = 'Ak'
for c in s:
s = ord(c)
s = '%03d'%s
mgk = (''.join(str(s)))
s = [mgk]
var = 1
s.insert(0, var)
mgc = lambda s: int(''.join(str(i) for i in s))
s = mgc(s)
print(s)
but then it counts each letter as its own element and it will not combine them and I only want the one in front of the very first number.
When the number is converted to an integer, it
Is this what you want? I am kinda confused:
a = 'Ak'
result = '1' + ''.join(str(f'{ord(char):03d}') for char in a)
print(result) # 1065107
# to make it a number just do:
my_int = int(result)
Lets say with a string = "AABBAAAAABBBBAAABBBBAA"
I want to return string split by the odd lengths of the string (i.e when A = 5 or A = 3),
What I want returned is 1) AABBAAAAA 2)BBBBAAA 3)BBBBAA,
How can I do that?
I tried using regex [A]+[B]+ for a slightly different case
One option might be to regex iterate using re.finditer with the following pattern:
.*?(?:AAA(?:AA)?|$)
This pattern will non greedily consume until reaching either 3 A's, 5 A's, or the end of the string. Then, we can print out each complete match as we iterate.
input = 'AABBAAAAABBBBAAABBBBAA'
pattern = '.*?(?:AAA(?:AA)?|$)'
for match in re.finditer(pattern, input):
print match.group()
This prints:
AABBAAAAA
BBBBAAA
BBBBAA
You can use itertools.groupby:
s = 'BBAAAAABBBBAAABBBBAA'
from itertools import groupby
out = ['']
for v, g in groupby(s):
l = [*g]
out[-1] += ''.join(l)
if v == 'A' and len(l) in (3, 5):
out.append('')
print(out)
Prints:
['BBAAAAA', 'BBBBAAA', 'BBBBAA']
I would like to know how to replace non-numeric characters in a single string with different random integers.
I have tried the following:
text = '1$1#387'
rec_1 = re.sub("\D+",str(random.randint(0,9)),text)
It then produced:
output: 1717387
As you can see, the non-numeric characters have been replaced by the same integer. I would like each non-numeric character to be replaced by a different integer. For example:
desired output: 1714387
Please assist.
Use a function as the replacement value:
def replacement(match):
return str(random.randint(0, 9))
text = '1$1#387'
rec_1 = re.sub(r"\D", replacement, text)
rec_1 is now "1011387", or "1511387", ...
That's because the randint function is called only 1 time.
You can use a lambda to get a new randint each time:
rec_1 = re.sub("\D+", lambda x: str(random.randint(0, 9)), text)