python: extract float from a python list of string( AUD 31.99)
python: extract float from a python list of string( AUD 31.99).
I used openpyxl to read from an excel file the amount list. and i saved it in a list but the list is in string form like this:
['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
I need to get the float from the string item list so that i can later save it in a new list to get the total of them.
Desired output:
[31.40, 32.99, 37.24]
I have already tried these:
newList = re.findall("d+.d+", tot[0])
print(newList)
Output:
[31.40]
But How can I use this for all the item elements?
I am new to python, this is just for some work i do, wanted to see the total using python instead of using excel`s find & replace option.
thanks
python regex excel list openpyxl
|
show 2 more comments
python: extract float from a python list of string( AUD 31.99).
I used openpyxl to read from an excel file the amount list. and i saved it in a list but the list is in string form like this:
['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
I need to get the float from the string item list so that i can later save it in a new list to get the total of them.
Desired output:
[31.40, 32.99, 37.24]
I have already tried these:
newList = re.findall("d+.d+", tot[0])
print(newList)
Output:
[31.40]
But How can I use this for all the item elements?
I am new to python, this is just for some work i do, wanted to see the total using python instead of using excel`s find & replace option.
thanks
python regex excel list openpyxl
Take a look here: book.pythontips.com/en/latest/map_filter.html
– Julio
Nov 13 '18 at 8:24
1
You should do the conversion before creating the list.
– Charlie Clark
Nov 13 '18 at 8:25
You are close.. but you are just passing tot[0].
– Anton vBR
Nov 13 '18 at 8:26
3
try[item.split()[0] for item in ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']]
– Andersson
Nov 13 '18 at 8:26
1
Is it mandatory to use regex?
– Vasilis G.
Nov 13 '18 at 8:28
|
show 2 more comments
python: extract float from a python list of string( AUD 31.99).
I used openpyxl to read from an excel file the amount list. and i saved it in a list but the list is in string form like this:
['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
I need to get the float from the string item list so that i can later save it in a new list to get the total of them.
Desired output:
[31.40, 32.99, 37.24]
I have already tried these:
newList = re.findall("d+.d+", tot[0])
print(newList)
Output:
[31.40]
But How can I use this for all the item elements?
I am new to python, this is just for some work i do, wanted to see the total using python instead of using excel`s find & replace option.
thanks
python regex excel list openpyxl
python: extract float from a python list of string( AUD 31.99).
I used openpyxl to read from an excel file the amount list. and i saved it in a list but the list is in string form like this:
['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
I need to get the float from the string item list so that i can later save it in a new list to get the total of them.
Desired output:
[31.40, 32.99, 37.24]
I have already tried these:
newList = re.findall("d+.d+", tot[0])
print(newList)
Output:
[31.40]
But How can I use this for all the item elements?
I am new to python, this is just for some work i do, wanted to see the total using python instead of using excel`s find & replace option.
thanks
python regex excel list openpyxl
python regex excel list openpyxl
edited Nov 13 '18 at 8:26
Vasilis G.
3,4082722
3,4082722
asked Nov 13 '18 at 8:22
Anamul ChoudhuryAnamul Choudhury
266
266
Take a look here: book.pythontips.com/en/latest/map_filter.html
– Julio
Nov 13 '18 at 8:24
1
You should do the conversion before creating the list.
– Charlie Clark
Nov 13 '18 at 8:25
You are close.. but you are just passing tot[0].
– Anton vBR
Nov 13 '18 at 8:26
3
try[item.split()[0] for item in ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']]
– Andersson
Nov 13 '18 at 8:26
1
Is it mandatory to use regex?
– Vasilis G.
Nov 13 '18 at 8:28
|
show 2 more comments
Take a look here: book.pythontips.com/en/latest/map_filter.html
– Julio
Nov 13 '18 at 8:24
1
You should do the conversion before creating the list.
– Charlie Clark
Nov 13 '18 at 8:25
You are close.. but you are just passing tot[0].
– Anton vBR
Nov 13 '18 at 8:26
3
try[item.split()[0] for item in ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']]
– Andersson
Nov 13 '18 at 8:26
1
Is it mandatory to use regex?
– Vasilis G.
Nov 13 '18 at 8:28
Take a look here: book.pythontips.com/en/latest/map_filter.html
– Julio
Nov 13 '18 at 8:24
Take a look here: book.pythontips.com/en/latest/map_filter.html
– Julio
Nov 13 '18 at 8:24
1
1
You should do the conversion before creating the list.
– Charlie Clark
Nov 13 '18 at 8:25
You should do the conversion before creating the list.
– Charlie Clark
Nov 13 '18 at 8:25
You are close.. but you are just passing tot[0].
– Anton vBR
Nov 13 '18 at 8:26
You are close.. but you are just passing tot[0].
– Anton vBR
Nov 13 '18 at 8:26
3
3
try
[item.split()[0] for item in ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']]
– Andersson
Nov 13 '18 at 8:26
try
[item.split()[0] for item in ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']]
– Andersson
Nov 13 '18 at 8:26
1
1
Is it mandatory to use regex?
– Vasilis G.
Nov 13 '18 at 8:28
Is it mandatory to use regex?
– Vasilis G.
Nov 13 '18 at 8:28
|
show 2 more comments
4 Answers
4
active
oldest
votes
You can use the map
function:
inList = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
output = list(map(lambda elem: float(elem.split()[0]), inList))
print(output)
Output:
[31.4, 32.99, 37.24]
This one solved the problem, Thanks alot.
– Anamul Choudhury
Nov 13 '18 at 8:36
@AnamulChoudhury you're welcome. Feel free to accept it as an answer if it helped you :)
– Vasilis G.
Nov 13 '18 at 8:48
add a comment |
If you want to get list of values with regex, try
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(re.search('d+.d+', fl).group(0)) for fl in tot]
print(newList)
# [31.40, 32.99, 37.24]
but using split
seem to be easier solution in this case
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(item.split()[0]) for item in tot]
print(newList)
# [31.40, 32.99, 37.24]
If second substring is always the same ("AUD"
) you can also try
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(item.rstrip(' AUD')) for item in tot]
print(newList)
# [31.40, 32.99, 37.24]
add a comment |
Is it possible to use a string split instead? I think it would be much simpler
ls1 = ['32.46 AUD', '17.34 AUD']
myFloats =
for aString in ls1:
aFloat = float(aString.split()[0])
myFloats.append(aFloat)
add a comment |
You should consider handling errors. Here is one way for instance:
import re
import math
def float_from_string(str_):
# Try to extract a floating number, if fail return nan
r = re.search('d+.d+', str_)
return float(r.group()) if r else math.nan
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD', ' nonumberhere AUD']
totfloat = [float_from_string(i) for i in tot]
print(totfloat)
Returns:
[31.4, 32.99, 37.24, nan]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53276670%2fpython-extract-float-from-a-python-list-of-string-aud-31-99%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the map
function:
inList = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
output = list(map(lambda elem: float(elem.split()[0]), inList))
print(output)
Output:
[31.4, 32.99, 37.24]
This one solved the problem, Thanks alot.
– Anamul Choudhury
Nov 13 '18 at 8:36
@AnamulChoudhury you're welcome. Feel free to accept it as an answer if it helped you :)
– Vasilis G.
Nov 13 '18 at 8:48
add a comment |
You can use the map
function:
inList = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
output = list(map(lambda elem: float(elem.split()[0]), inList))
print(output)
Output:
[31.4, 32.99, 37.24]
This one solved the problem, Thanks alot.
– Anamul Choudhury
Nov 13 '18 at 8:36
@AnamulChoudhury you're welcome. Feel free to accept it as an answer if it helped you :)
– Vasilis G.
Nov 13 '18 at 8:48
add a comment |
You can use the map
function:
inList = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
output = list(map(lambda elem: float(elem.split()[0]), inList))
print(output)
Output:
[31.4, 32.99, 37.24]
You can use the map
function:
inList = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
output = list(map(lambda elem: float(elem.split()[0]), inList))
print(output)
Output:
[31.4, 32.99, 37.24]
answered Nov 13 '18 at 8:31
Vasilis G.Vasilis G.
3,4082722
3,4082722
This one solved the problem, Thanks alot.
– Anamul Choudhury
Nov 13 '18 at 8:36
@AnamulChoudhury you're welcome. Feel free to accept it as an answer if it helped you :)
– Vasilis G.
Nov 13 '18 at 8:48
add a comment |
This one solved the problem, Thanks alot.
– Anamul Choudhury
Nov 13 '18 at 8:36
@AnamulChoudhury you're welcome. Feel free to accept it as an answer if it helped you :)
– Vasilis G.
Nov 13 '18 at 8:48
This one solved the problem, Thanks alot.
– Anamul Choudhury
Nov 13 '18 at 8:36
This one solved the problem, Thanks alot.
– Anamul Choudhury
Nov 13 '18 at 8:36
@AnamulChoudhury you're welcome. Feel free to accept it as an answer if it helped you :)
– Vasilis G.
Nov 13 '18 at 8:48
@AnamulChoudhury you're welcome. Feel free to accept it as an answer if it helped you :)
– Vasilis G.
Nov 13 '18 at 8:48
add a comment |
If you want to get list of values with regex, try
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(re.search('d+.d+', fl).group(0)) for fl in tot]
print(newList)
# [31.40, 32.99, 37.24]
but using split
seem to be easier solution in this case
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(item.split()[0]) for item in tot]
print(newList)
# [31.40, 32.99, 37.24]
If second substring is always the same ("AUD"
) you can also try
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(item.rstrip(' AUD')) for item in tot]
print(newList)
# [31.40, 32.99, 37.24]
add a comment |
If you want to get list of values with regex, try
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(re.search('d+.d+', fl).group(0)) for fl in tot]
print(newList)
# [31.40, 32.99, 37.24]
but using split
seem to be easier solution in this case
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(item.split()[0]) for item in tot]
print(newList)
# [31.40, 32.99, 37.24]
If second substring is always the same ("AUD"
) you can also try
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(item.rstrip(' AUD')) for item in tot]
print(newList)
# [31.40, 32.99, 37.24]
add a comment |
If you want to get list of values with regex, try
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(re.search('d+.d+', fl).group(0)) for fl in tot]
print(newList)
# [31.40, 32.99, 37.24]
but using split
seem to be easier solution in this case
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(item.split()[0]) for item in tot]
print(newList)
# [31.40, 32.99, 37.24]
If second substring is always the same ("AUD"
) you can also try
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(item.rstrip(' AUD')) for item in tot]
print(newList)
# [31.40, 32.99, 37.24]
If you want to get list of values with regex, try
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(re.search('d+.d+', fl).group(0)) for fl in tot]
print(newList)
# [31.40, 32.99, 37.24]
but using split
seem to be easier solution in this case
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(item.split()[0]) for item in tot]
print(newList)
# [31.40, 32.99, 37.24]
If second substring is always the same ("AUD"
) you can also try
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
newList = [float(item.rstrip(' AUD')) for item in tot]
print(newList)
# [31.40, 32.99, 37.24]
edited Nov 13 '18 at 8:39
answered Nov 13 '18 at 8:33
AnderssonAndersson
37.6k103266
37.6k103266
add a comment |
add a comment |
Is it possible to use a string split instead? I think it would be much simpler
ls1 = ['32.46 AUD', '17.34 AUD']
myFloats =
for aString in ls1:
aFloat = float(aString.split()[0])
myFloats.append(aFloat)
add a comment |
Is it possible to use a string split instead? I think it would be much simpler
ls1 = ['32.46 AUD', '17.34 AUD']
myFloats =
for aString in ls1:
aFloat = float(aString.split()[0])
myFloats.append(aFloat)
add a comment |
Is it possible to use a string split instead? I think it would be much simpler
ls1 = ['32.46 AUD', '17.34 AUD']
myFloats =
for aString in ls1:
aFloat = float(aString.split()[0])
myFloats.append(aFloat)
Is it possible to use a string split instead? I think it would be much simpler
ls1 = ['32.46 AUD', '17.34 AUD']
myFloats =
for aString in ls1:
aFloat = float(aString.split()[0])
myFloats.append(aFloat)
answered Nov 13 '18 at 8:28
DavidhallDavidhall
259
259
add a comment |
add a comment |
You should consider handling errors. Here is one way for instance:
import re
import math
def float_from_string(str_):
# Try to extract a floating number, if fail return nan
r = re.search('d+.d+', str_)
return float(r.group()) if r else math.nan
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD', ' nonumberhere AUD']
totfloat = [float_from_string(i) for i in tot]
print(totfloat)
Returns:
[31.4, 32.99, 37.24, nan]
add a comment |
You should consider handling errors. Here is one way for instance:
import re
import math
def float_from_string(str_):
# Try to extract a floating number, if fail return nan
r = re.search('d+.d+', str_)
return float(r.group()) if r else math.nan
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD', ' nonumberhere AUD']
totfloat = [float_from_string(i) for i in tot]
print(totfloat)
Returns:
[31.4, 32.99, 37.24, nan]
add a comment |
You should consider handling errors. Here is one way for instance:
import re
import math
def float_from_string(str_):
# Try to extract a floating number, if fail return nan
r = re.search('d+.d+', str_)
return float(r.group()) if r else math.nan
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD', ' nonumberhere AUD']
totfloat = [float_from_string(i) for i in tot]
print(totfloat)
Returns:
[31.4, 32.99, 37.24, nan]
You should consider handling errors. Here is one way for instance:
import re
import math
def float_from_string(str_):
# Try to extract a floating number, if fail return nan
r = re.search('d+.d+', str_)
return float(r.group()) if r else math.nan
tot = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD', ' nonumberhere AUD']
totfloat = [float_from_string(i) for i in tot]
print(totfloat)
Returns:
[31.4, 32.99, 37.24, nan]
answered Nov 13 '18 at 8:37
Anton vBRAnton vBR
11.3k21022
11.3k21022
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%2f53276670%2fpython-extract-float-from-a-python-list-of-string-aud-31-99%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
Take a look here: book.pythontips.com/en/latest/map_filter.html
– Julio
Nov 13 '18 at 8:24
1
You should do the conversion before creating the list.
– Charlie Clark
Nov 13 '18 at 8:25
You are close.. but you are just passing tot[0].
– Anton vBR
Nov 13 '18 at 8:26
3
try
[item.split()[0] for item in ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']]
– Andersson
Nov 13 '18 at 8:26
1
Is it mandatory to use regex?
– Vasilis G.
Nov 13 '18 at 8:28