python: extract float from a python list of string( AUD 31.99)












2














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










share|improve this question
























  • 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
















2














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










share|improve this question
























  • 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














2












2








2







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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












4 Answers
4






active

oldest

votes


















1














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]





share|improve this answer





















  • 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



















1














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]





share|improve this answer































    0














    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)





    share|improve this answer





























      0














      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]





      share|improve this answer





















        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        1














        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]





        share|improve this answer





















        • 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
















        1














        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]





        share|improve this answer





















        • 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














        1












        1








        1






        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]





        share|improve this answer












        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]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        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


















        • 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













        1














        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]





        share|improve this answer




























          1














          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]





          share|improve this answer


























            1












            1








            1






            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]





            share|improve this answer














            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]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 8:39

























            answered Nov 13 '18 at 8:33









            AnderssonAndersson

            37.6k103266




            37.6k103266























                0














                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)





                share|improve this answer


























                  0














                  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)





                  share|improve this answer
























                    0












                    0








                    0






                    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)





                    share|improve this answer












                    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)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 8:28









                    DavidhallDavidhall

                    259




                    259























                        0














                        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]





                        share|improve this answer


























                          0














                          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]





                          share|improve this answer
























                            0












                            0








                            0






                            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]





                            share|improve this answer












                            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]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 8:37









                            Anton vBRAnton vBR

                            11.3k21022




                            11.3k21022






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Bressuire

                                Vorschmack

                                Quarantine