Python: Multiply a number in a string by a constant
up vote
2
down vote
favorite
I am trying to multiply by a constant a series of number in a string. I have something like that:
my_string = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'
I can easily do this:
my_new_string = re.sub('(?<=AMOUNT=")d+', lambda _:str(38000 *2), my_string)
This works, but that is not what I need.
What I need as an output as the following:
my_new_string = 'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'
Does anyone have an idea on how to proceed? Thank you
python string
add a comment |
up vote
2
down vote
favorite
I am trying to multiply by a constant a series of number in a string. I have something like that:
my_string = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'
I can easily do this:
my_new_string = re.sub('(?<=AMOUNT=")d+', lambda _:str(38000 *2), my_string)
This works, but that is not what I need.
What I need as an output as the following:
my_new_string = 'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'
Does anyone have an idea on how to proceed? Thank you
python string
Why did you put38000 *2
into your regexp?
– Psytho
Nov 19 at 14:28
1
It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
– Serge Ballesta
Nov 19 at 14:28
Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
– WhelanG
Nov 19 at 14:30
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to multiply by a constant a series of number in a string. I have something like that:
my_string = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'
I can easily do this:
my_new_string = re.sub('(?<=AMOUNT=")d+', lambda _:str(38000 *2), my_string)
This works, but that is not what I need.
What I need as an output as the following:
my_new_string = 'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'
Does anyone have an idea on how to proceed? Thank you
python string
I am trying to multiply by a constant a series of number in a string. I have something like that:
my_string = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'
I can easily do this:
my_new_string = re.sub('(?<=AMOUNT=")d+', lambda _:str(38000 *2), my_string)
This works, but that is not what I need.
What I need as an output as the following:
my_new_string = 'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'
Does anyone have an idea on how to proceed? Thank you
python string
python string
edited Nov 19 at 14:23
asked Nov 19 at 14:21
WhelanG
687
687
Why did you put38000 *2
into your regexp?
– Psytho
Nov 19 at 14:28
1
It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
– Serge Ballesta
Nov 19 at 14:28
Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
– WhelanG
Nov 19 at 14:30
add a comment |
Why did you put38000 *2
into your regexp?
– Psytho
Nov 19 at 14:28
1
It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
– Serge Ballesta
Nov 19 at 14:28
Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
– WhelanG
Nov 19 at 14:30
Why did you put
38000 *2
into your regexp?– Psytho
Nov 19 at 14:28
Why did you put
38000 *2
into your regexp?– Psytho
Nov 19 at 14:28
1
1
It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
– Serge Ballesta
Nov 19 at 14:28
It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
– Serge Ballesta
Nov 19 at 14:28
Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
– WhelanG
Nov 19 at 14:30
Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
– WhelanG
Nov 19 at 14:30
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
You just need to make a little modification:
re.sub('(?<=AMOUNT=")d+', lambda match: str(int(match.group()) * 2), my_string)
The object being passed into lambda
is the re.Match
object with number you needed. Now you just need to extract the number with .group()
method, convert to int * 2
and convert back to str
.
Thanks a lot, this is what I was missing
– WhelanG
Nov 19 at 14:38
add a comment |
up vote
2
down vote
You can access the matching group in the lambda, as
re.sub('(?<=AMOUNT=")d+', lambda m:str(int(m.group(0))*2), my_string)
add a comment |
up vote
0
down vote
You can define a custom function and use f-strings:
def splitter(x):
key, val = x.split('=')
return f'{key}="{int(val[1:-1])*2}"'
mystr = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'
res = ' '.join(map(splitter, mystr.split()))
'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You just need to make a little modification:
re.sub('(?<=AMOUNT=")d+', lambda match: str(int(match.group()) * 2), my_string)
The object being passed into lambda
is the re.Match
object with number you needed. Now you just need to extract the number with .group()
method, convert to int * 2
and convert back to str
.
Thanks a lot, this is what I was missing
– WhelanG
Nov 19 at 14:38
add a comment |
up vote
2
down vote
accepted
You just need to make a little modification:
re.sub('(?<=AMOUNT=")d+', lambda match: str(int(match.group()) * 2), my_string)
The object being passed into lambda
is the re.Match
object with number you needed. Now you just need to extract the number with .group()
method, convert to int * 2
and convert back to str
.
Thanks a lot, this is what I was missing
– WhelanG
Nov 19 at 14:38
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You just need to make a little modification:
re.sub('(?<=AMOUNT=")d+', lambda match: str(int(match.group()) * 2), my_string)
The object being passed into lambda
is the re.Match
object with number you needed. Now you just need to extract the number with .group()
method, convert to int * 2
and convert back to str
.
You just need to make a little modification:
re.sub('(?<=AMOUNT=")d+', lambda match: str(int(match.group()) * 2), my_string)
The object being passed into lambda
is the re.Match
object with number you needed. Now you just need to extract the number with .group()
method, convert to int * 2
and convert back to str
.
answered Nov 19 at 14:33
Idlehands
3,8571417
3,8571417
Thanks a lot, this is what I was missing
– WhelanG
Nov 19 at 14:38
add a comment |
Thanks a lot, this is what I was missing
– WhelanG
Nov 19 at 14:38
Thanks a lot, this is what I was missing
– WhelanG
Nov 19 at 14:38
Thanks a lot, this is what I was missing
– WhelanG
Nov 19 at 14:38
add a comment |
up vote
2
down vote
You can access the matching group in the lambda, as
re.sub('(?<=AMOUNT=")d+', lambda m:str(int(m.group(0))*2), my_string)
add a comment |
up vote
2
down vote
You can access the matching group in the lambda, as
re.sub('(?<=AMOUNT=")d+', lambda m:str(int(m.group(0))*2), my_string)
add a comment |
up vote
2
down vote
up vote
2
down vote
You can access the matching group in the lambda, as
re.sub('(?<=AMOUNT=")d+', lambda m:str(int(m.group(0))*2), my_string)
You can access the matching group in the lambda, as
re.sub('(?<=AMOUNT=")d+', lambda m:str(int(m.group(0))*2), my_string)
answered Nov 19 at 14:34
functor
645
645
add a comment |
add a comment |
up vote
0
down vote
You can define a custom function and use f-strings:
def splitter(x):
key, val = x.split('=')
return f'{key}="{int(val[1:-1])*2}"'
mystr = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'
res = ' '.join(map(splitter, mystr.split()))
'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'
add a comment |
up vote
0
down vote
You can define a custom function and use f-strings:
def splitter(x):
key, val = x.split('=')
return f'{key}="{int(val[1:-1])*2}"'
mystr = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'
res = ' '.join(map(splitter, mystr.split()))
'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'
add a comment |
up vote
0
down vote
up vote
0
down vote
You can define a custom function and use f-strings:
def splitter(x):
key, val = x.split('=')
return f'{key}="{int(val[1:-1])*2}"'
mystr = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'
res = ' '.join(map(splitter, mystr.split()))
'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'
You can define a custom function and use f-strings:
def splitter(x):
key, val = x.split('=')
return f'{key}="{int(val[1:-1])*2}"'
mystr = 'AMOUNT="38000" AMOUNT="43000" AMOUNT="56399"'
res = ' '.join(map(splitter, mystr.split()))
'AMOUNT="76000" AMOUNT="86000" AMOUNT="112798"'
answered Nov 19 at 14:25
jpp
86.7k194998
86.7k194998
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376627%2fpython-multiply-a-number-in-a-string-by-a-constant%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why did you put
38000 *2
into your regexp?– Psytho
Nov 19 at 14:28
1
It is certainly possible, but IMHO it would be much better to compute the values BEFORE writing the string.
– Serge Ballesta
Nov 19 at 14:28
Yes, but I am just simplifying. The actual case involves a huge string coming from an xml file in which I need to multiply by 2 the values in it.
– WhelanG
Nov 19 at 14:30