joining output from regex search












5
















  • I have a regex that looks for numbers in a file.

  • I put results in a list


The problem is that it prints each results on a new line for every single number it finds. it aslo ignore the list I've created.



What I want to do is to have all the numbers into one list.
I used join() but it doesn't works.



code :



def readfile():
regex = re.compile('d+')
for num in regex.findall(open('/path/to/file').read()):
lst = [num]
jn = ''.join(lst)
print(jn)


output :



122
34
764









share|improve this question





























    5
















    • I have a regex that looks for numbers in a file.

    • I put results in a list


    The problem is that it prints each results on a new line for every single number it finds. it aslo ignore the list I've created.



    What I want to do is to have all the numbers into one list.
    I used join() but it doesn't works.



    code :



    def readfile():
    regex = re.compile('d+')
    for num in regex.findall(open('/path/to/file').read()):
    lst = [num]
    jn = ''.join(lst)
    print(jn)


    output :



    122
    34
    764









    share|improve this question



























      5












      5








      5


      1







      • I have a regex that looks for numbers in a file.

      • I put results in a list


      The problem is that it prints each results on a new line for every single number it finds. it aslo ignore the list I've created.



      What I want to do is to have all the numbers into one list.
      I used join() but it doesn't works.



      code :



      def readfile():
      regex = re.compile('d+')
      for num in regex.findall(open('/path/to/file').read()):
      lst = [num]
      jn = ''.join(lst)
      print(jn)


      output :



      122
      34
      764









      share|improve this question

















      • I have a regex that looks for numbers in a file.

      • I put results in a list


      The problem is that it prints each results on a new line for every single number it finds. it aslo ignore the list I've created.



      What I want to do is to have all the numbers into one list.
      I used join() but it doesn't works.



      code :



      def readfile():
      regex = re.compile('d+')
      for num in regex.findall(open('/path/to/file').read()):
      lst = [num]
      jn = ''.join(lst)
      print(jn)


      output :



      122
      34
      764






      python python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 10:59









      Patrick Artner

      24.6k62443




      24.6k62443










      asked Jan 1 at 10:40









      shimoshimo

      283




      283
























          3 Answers
          3






          active

          oldest

          votes


















          1














          In your case, regex.findall() returns a list and you are are joining in each iteration and printing it.



          That is why you're seeing this problem.



          You can try something like this.




          numbers.txt




          Xy10Ab
          Tiger20
          Beta30Man
          56
          My45one



          statements:




          >>> import re
          >>>
          >>> regex = re.compile(r'd+')
          >>> lst =
          >>>
          >>> for num in regex.findall(open('numbers.txt').read()):
          ... lst.append(num)
          ...
          >>> lst
          ['10', '20', '30', '56', '45']
          >>>
          >>> jn = ''.join(lst)
          >>>
          >>> jn
          '1020305645'
          >>>
          >>> jn2 = 'n'.join(lst)
          >>> jn2
          '10n20n30n56n45'
          >>>
          >>> print(jn2)
          10
          20
          30
          56
          45
          >>>
          >>> nums = [int(n) for n in lst]
          >>> nums
          [10, 20, 30, 56, 45]
          >>>
          >>> sum(nums)
          161
          >>>





          share|improve this answer

































            3














            What goes wrong:




            # this iterates the single numbers you find - one by one
            for num in regex.findall(open('/path/to/file').read()):
            lst = [num] # this puts one number back into a new list
            jn = ''.join(lst) # this gets the number back out of the new list
            print(jn) # this prints one number



            Fixing it:



            Reading re.findall() show's you, it returns a list already.



            There is no(t much) need to use a for on it to print it.



            If you want a list - simply use re.findall()'s return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):



            import re

            my_r = re.compile(r'd+') # define pattern as raw-string

            numbers = my_r.findall("123 456 789") # get the list

            print(numbers)

            # different methods to print a list on one line
            # adjust sep / end to fit your needs
            print( *numbers, sep=", ") # print #1

            for n in numbers[:-1]: # print #2
            print(n, end = ", ")
            print(numbers[-1])

            print(', '.join(numbers)) # print #3


            Output:



            ['123', '456', '789']   # list of found strings that are numbers
            123, 456, 789
            123, 456, 789
            123, 456, 789


            Doku:




            • print() function for sep= and end=

            • Printing an int list in a single line python3


            • Convert all strings in a list to int ... if you need the list as numbers




            More on printing in one line:




            • Print in one line dynamically

            • Python: multiple prints on the same line

            • How to print without newline or space?

            • Print new output on same line






            share|improve this answer

































              -1














              Use list built-in functions to append new values.



              def readfile():
              regex = re.compile('d+')
              lst =

              for num in regex.findall(open('/path/to/file').read()):
              lst.append(num)

              print(lst)





              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%2f53994798%2fjoining-output-from-regex-search%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                In your case, regex.findall() returns a list and you are are joining in each iteration and printing it.



                That is why you're seeing this problem.



                You can try something like this.




                numbers.txt




                Xy10Ab
                Tiger20
                Beta30Man
                56
                My45one



                statements:




                >>> import re
                >>>
                >>> regex = re.compile(r'd+')
                >>> lst =
                >>>
                >>> for num in regex.findall(open('numbers.txt').read()):
                ... lst.append(num)
                ...
                >>> lst
                ['10', '20', '30', '56', '45']
                >>>
                >>> jn = ''.join(lst)
                >>>
                >>> jn
                '1020305645'
                >>>
                >>> jn2 = 'n'.join(lst)
                >>> jn2
                '10n20n30n56n45'
                >>>
                >>> print(jn2)
                10
                20
                30
                56
                45
                >>>
                >>> nums = [int(n) for n in lst]
                >>> nums
                [10, 20, 30, 56, 45]
                >>>
                >>> sum(nums)
                161
                >>>





                share|improve this answer






























                  1














                  In your case, regex.findall() returns a list and you are are joining in each iteration and printing it.



                  That is why you're seeing this problem.



                  You can try something like this.




                  numbers.txt




                  Xy10Ab
                  Tiger20
                  Beta30Man
                  56
                  My45one



                  statements:




                  >>> import re
                  >>>
                  >>> regex = re.compile(r'd+')
                  >>> lst =
                  >>>
                  >>> for num in regex.findall(open('numbers.txt').read()):
                  ... lst.append(num)
                  ...
                  >>> lst
                  ['10', '20', '30', '56', '45']
                  >>>
                  >>> jn = ''.join(lst)
                  >>>
                  >>> jn
                  '1020305645'
                  >>>
                  >>> jn2 = 'n'.join(lst)
                  >>> jn2
                  '10n20n30n56n45'
                  >>>
                  >>> print(jn2)
                  10
                  20
                  30
                  56
                  45
                  >>>
                  >>> nums = [int(n) for n in lst]
                  >>> nums
                  [10, 20, 30, 56, 45]
                  >>>
                  >>> sum(nums)
                  161
                  >>>





                  share|improve this answer




























                    1












                    1








                    1







                    In your case, regex.findall() returns a list and you are are joining in each iteration and printing it.



                    That is why you're seeing this problem.



                    You can try something like this.




                    numbers.txt




                    Xy10Ab
                    Tiger20
                    Beta30Man
                    56
                    My45one



                    statements:




                    >>> import re
                    >>>
                    >>> regex = re.compile(r'd+')
                    >>> lst =
                    >>>
                    >>> for num in regex.findall(open('numbers.txt').read()):
                    ... lst.append(num)
                    ...
                    >>> lst
                    ['10', '20', '30', '56', '45']
                    >>>
                    >>> jn = ''.join(lst)
                    >>>
                    >>> jn
                    '1020305645'
                    >>>
                    >>> jn2 = 'n'.join(lst)
                    >>> jn2
                    '10n20n30n56n45'
                    >>>
                    >>> print(jn2)
                    10
                    20
                    30
                    56
                    45
                    >>>
                    >>> nums = [int(n) for n in lst]
                    >>> nums
                    [10, 20, 30, 56, 45]
                    >>>
                    >>> sum(nums)
                    161
                    >>>





                    share|improve this answer















                    In your case, regex.findall() returns a list and you are are joining in each iteration and printing it.



                    That is why you're seeing this problem.



                    You can try something like this.




                    numbers.txt




                    Xy10Ab
                    Tiger20
                    Beta30Man
                    56
                    My45one



                    statements:




                    >>> import re
                    >>>
                    >>> regex = re.compile(r'd+')
                    >>> lst =
                    >>>
                    >>> for num in regex.findall(open('numbers.txt').read()):
                    ... lst.append(num)
                    ...
                    >>> lst
                    ['10', '20', '30', '56', '45']
                    >>>
                    >>> jn = ''.join(lst)
                    >>>
                    >>> jn
                    '1020305645'
                    >>>
                    >>> jn2 = 'n'.join(lst)
                    >>> jn2
                    '10n20n30n56n45'
                    >>>
                    >>> print(jn2)
                    10
                    20
                    30
                    56
                    45
                    >>>
                    >>> nums = [int(n) for n in lst]
                    >>> nums
                    [10, 20, 30, 56, 45]
                    >>>
                    >>> sum(nums)
                    161
                    >>>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 1 at 11:42

























                    answered Jan 1 at 11:26









                    hygullhygull

                    3,67021431




                    3,67021431

























                        3














                        What goes wrong:




                        # this iterates the single numbers you find - one by one
                        for num in regex.findall(open('/path/to/file').read()):
                        lst = [num] # this puts one number back into a new list
                        jn = ''.join(lst) # this gets the number back out of the new list
                        print(jn) # this prints one number



                        Fixing it:



                        Reading re.findall() show's you, it returns a list already.



                        There is no(t much) need to use a for on it to print it.



                        If you want a list - simply use re.findall()'s return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):



                        import re

                        my_r = re.compile(r'd+') # define pattern as raw-string

                        numbers = my_r.findall("123 456 789") # get the list

                        print(numbers)

                        # different methods to print a list on one line
                        # adjust sep / end to fit your needs
                        print( *numbers, sep=", ") # print #1

                        for n in numbers[:-1]: # print #2
                        print(n, end = ", ")
                        print(numbers[-1])

                        print(', '.join(numbers)) # print #3


                        Output:



                        ['123', '456', '789']   # list of found strings that are numbers
                        123, 456, 789
                        123, 456, 789
                        123, 456, 789


                        Doku:




                        • print() function for sep= and end=

                        • Printing an int list in a single line python3


                        • Convert all strings in a list to int ... if you need the list as numbers




                        More on printing in one line:




                        • Print in one line dynamically

                        • Python: multiple prints on the same line

                        • How to print without newline or space?

                        • Print new output on same line






                        share|improve this answer






























                          3














                          What goes wrong:




                          # this iterates the single numbers you find - one by one
                          for num in regex.findall(open('/path/to/file').read()):
                          lst = [num] # this puts one number back into a new list
                          jn = ''.join(lst) # this gets the number back out of the new list
                          print(jn) # this prints one number



                          Fixing it:



                          Reading re.findall() show's you, it returns a list already.



                          There is no(t much) need to use a for on it to print it.



                          If you want a list - simply use re.findall()'s return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):



                          import re

                          my_r = re.compile(r'd+') # define pattern as raw-string

                          numbers = my_r.findall("123 456 789") # get the list

                          print(numbers)

                          # different methods to print a list on one line
                          # adjust sep / end to fit your needs
                          print( *numbers, sep=", ") # print #1

                          for n in numbers[:-1]: # print #2
                          print(n, end = ", ")
                          print(numbers[-1])

                          print(', '.join(numbers)) # print #3


                          Output:



                          ['123', '456', '789']   # list of found strings that are numbers
                          123, 456, 789
                          123, 456, 789
                          123, 456, 789


                          Doku:




                          • print() function for sep= and end=

                          • Printing an int list in a single line python3


                          • Convert all strings in a list to int ... if you need the list as numbers




                          More on printing in one line:




                          • Print in one line dynamically

                          • Python: multiple prints on the same line

                          • How to print without newline or space?

                          • Print new output on same line






                          share|improve this answer




























                            3












                            3








                            3







                            What goes wrong:




                            # this iterates the single numbers you find - one by one
                            for num in regex.findall(open('/path/to/file').read()):
                            lst = [num] # this puts one number back into a new list
                            jn = ''.join(lst) # this gets the number back out of the new list
                            print(jn) # this prints one number



                            Fixing it:



                            Reading re.findall() show's you, it returns a list already.



                            There is no(t much) need to use a for on it to print it.



                            If you want a list - simply use re.findall()'s return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):



                            import re

                            my_r = re.compile(r'd+') # define pattern as raw-string

                            numbers = my_r.findall("123 456 789") # get the list

                            print(numbers)

                            # different methods to print a list on one line
                            # adjust sep / end to fit your needs
                            print( *numbers, sep=", ") # print #1

                            for n in numbers[:-1]: # print #2
                            print(n, end = ", ")
                            print(numbers[-1])

                            print(', '.join(numbers)) # print #3


                            Output:



                            ['123', '456', '789']   # list of found strings that are numbers
                            123, 456, 789
                            123, 456, 789
                            123, 456, 789


                            Doku:




                            • print() function for sep= and end=

                            • Printing an int list in a single line python3


                            • Convert all strings in a list to int ... if you need the list as numbers




                            More on printing in one line:




                            • Print in one line dynamically

                            • Python: multiple prints on the same line

                            • How to print without newline or space?

                            • Print new output on same line






                            share|improve this answer















                            What goes wrong:




                            # this iterates the single numbers you find - one by one
                            for num in regex.findall(open('/path/to/file').read()):
                            lst = [num] # this puts one number back into a new list
                            jn = ''.join(lst) # this gets the number back out of the new list
                            print(jn) # this prints one number



                            Fixing it:



                            Reading re.findall() show's you, it returns a list already.



                            There is no(t much) need to use a for on it to print it.



                            If you want a list - simply use re.findall()'s return value - if you want to print it, use one of the methods in Printing an int list in a single line python3 (several more posts on SO about printing in one line):



                            import re

                            my_r = re.compile(r'd+') # define pattern as raw-string

                            numbers = my_r.findall("123 456 789") # get the list

                            print(numbers)

                            # different methods to print a list on one line
                            # adjust sep / end to fit your needs
                            print( *numbers, sep=", ") # print #1

                            for n in numbers[:-1]: # print #2
                            print(n, end = ", ")
                            print(numbers[-1])

                            print(', '.join(numbers)) # print #3


                            Output:



                            ['123', '456', '789']   # list of found strings that are numbers
                            123, 456, 789
                            123, 456, 789
                            123, 456, 789


                            Doku:




                            • print() function for sep= and end=

                            • Printing an int list in a single line python3


                            • Convert all strings in a list to int ... if you need the list as numbers




                            More on printing in one line:




                            • Print in one line dynamically

                            • Python: multiple prints on the same line

                            • How to print without newline or space?

                            • Print new output on same line







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 1 at 11:23

























                            answered Jan 1 at 11:09









                            Patrick ArtnerPatrick Artner

                            24.6k62443




                            24.6k62443























                                -1














                                Use list built-in functions to append new values.



                                def readfile():
                                regex = re.compile('d+')
                                lst =

                                for num in regex.findall(open('/path/to/file').read()):
                                lst.append(num)

                                print(lst)





                                share|improve this answer




























                                  -1














                                  Use list built-in functions to append new values.



                                  def readfile():
                                  regex = re.compile('d+')
                                  lst =

                                  for num in regex.findall(open('/path/to/file').read()):
                                  lst.append(num)

                                  print(lst)





                                  share|improve this answer


























                                    -1












                                    -1








                                    -1







                                    Use list built-in functions to append new values.



                                    def readfile():
                                    regex = re.compile('d+')
                                    lst =

                                    for num in regex.findall(open('/path/to/file').read()):
                                    lst.append(num)

                                    print(lst)





                                    share|improve this answer













                                    Use list built-in functions to append new values.



                                    def readfile():
                                    regex = re.compile('d+')
                                    lst =

                                    for num in regex.findall(open('/path/to/file').read()):
                                    lst.append(num)

                                    print(lst)






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 1 at 11:02









                                    ThunderMindThunderMind

                                    615113




                                    615113






























                                        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.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994798%2fjoining-output-from-regex-search%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

                                        Monofisismo

                                        Angular Downloading a file using contenturl with Basic Authentication

                                        Olmecas