Python : Select text between angle bracketsCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonDifference between __str__ and __repr__?Does Python have a string 'contains' substring method?

Blender - show edges angles “direction”

Why is delta-v is the most useful quantity for planning space travel?

Stereotypical names

Is the next prime number always the next number divisible by the current prime number, except for any numbers previously divisible by primes?

How to check participants in at events?

Bob has never been a M before

How will losing mobility of one hand affect my career as a programmer?

Is it possible to build a CPA Secure encryption scheme which remains secure even when the encryption of secret key is given?

How to interpret the phrase "t’en a fait voir à toi"?

What (else) happened July 1st 1858 in London?

Should my PhD thesis be submitted under my legal name?

Are taller landing gear bad for aircraft, particulary large airliners?

What is the opposite of 'gravitas'?

Can I Retrieve Email Addresses from BCC?

Why are all the doors on Ferenginar (the Ferengi home world) far shorter than the average Ferengi?

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?

Can a malicious addon access internet history and such in chrome/firefox?

Can I use my Chinese passport to enter China after I acquired another citizenship?

Indicating multiple different modes of speech (fantasy language or telepathy)

Would it be legal for a US State to ban exports of a natural resource?

Is infinity mathematically observable?

The One-Electron Universe postulate is true - what simple change can I make to change the whole universe?

Freedom of speech and where it applies

Installing PowerShell on 32-bit Kali OS fails



Python : Select text between angle brackets


Calling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonDifference between __str__ and __repr__?Does Python have a string 'contains' substring method?













1















I read a csv file and convert it to a pandas dataframe, with 2 text columns. In one column I have multiple rows of this form:



<suggested-actions-list text =""is this a test?"">suggested- 
action>Yes</suggested-action><suggested-action>No</suggested-action>
</suggested-actions-list>"

<choice-list text=""some text""> <choice-option>option1</choice-option>
<choice-option>option2</choice-option> <choice-option>option3</choice-
option></choice-list>


I want to select the text between the angle brackets, in order to end up with something like this:



""is this a test?"" Yes No
""some text"" option1 option2 option3


Can someone give a hint? Thanks!










share|improve this question
























  • You can try beautifulsoup parser. You can also parse xml documents with it.

    – systrigger
    Mar 7 at 10:38















1















I read a csv file and convert it to a pandas dataframe, with 2 text columns. In one column I have multiple rows of this form:



<suggested-actions-list text =""is this a test?"">suggested- 
action>Yes</suggested-action><suggested-action>No</suggested-action>
</suggested-actions-list>"

<choice-list text=""some text""> <choice-option>option1</choice-option>
<choice-option>option2</choice-option> <choice-option>option3</choice-
option></choice-list>


I want to select the text between the angle brackets, in order to end up with something like this:



""is this a test?"" Yes No
""some text"" option1 option2 option3


Can someone give a hint? Thanks!










share|improve this question
























  • You can try beautifulsoup parser. You can also parse xml documents with it.

    – systrigger
    Mar 7 at 10:38













1












1








1


0






I read a csv file and convert it to a pandas dataframe, with 2 text columns. In one column I have multiple rows of this form:



<suggested-actions-list text =""is this a test?"">suggested- 
action>Yes</suggested-action><suggested-action>No</suggested-action>
</suggested-actions-list>"

<choice-list text=""some text""> <choice-option>option1</choice-option>
<choice-option>option2</choice-option> <choice-option>option3</choice-
option></choice-list>


I want to select the text between the angle brackets, in order to end up with something like this:



""is this a test?"" Yes No
""some text"" option1 option2 option3


Can someone give a hint? Thanks!










share|improve this question
















I read a csv file and convert it to a pandas dataframe, with 2 text columns. In one column I have multiple rows of this form:



<suggested-actions-list text =""is this a test?"">suggested- 
action>Yes</suggested-action><suggested-action>No</suggested-action>
</suggested-actions-list>"

<choice-list text=""some text""> <choice-option>option1</choice-option>
<choice-option>option2</choice-option> <choice-option>option3</choice-
option></choice-list>


I want to select the text between the angle brackets, in order to end up with something like this:



""is this a test?"" Yes No
""some text"" option1 option2 option3


Can someone give a hint? Thanks!







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 10:30







joasa

















asked Mar 7 at 10:00









joasajoasa

187218




187218












  • You can try beautifulsoup parser. You can also parse xml documents with it.

    – systrigger
    Mar 7 at 10:38

















  • You can try beautifulsoup parser. You can also parse xml documents with it.

    – systrigger
    Mar 7 at 10:38
















You can try beautifulsoup parser. You can also parse xml documents with it.

– systrigger
Mar 7 at 10:38





You can try beautifulsoup parser. You can also parse xml documents with it.

– systrigger
Mar 7 at 10:38












2 Answers
2






active

oldest

votes


















1














s = """
<suggested-actions-list text =""is this a test?""><suggested-action>Yes</suggested-action><suggested-action>No</suggested-action></suggested-actions-list>

<choice-list text=""some text""> <choice-option>option1</choice-option><choice-option>option2</choice-option> <choice-option>option3</choice-option></choice-list>
"""

x = re.sub('<(?:.*?)("".*"")?>', r'1 ', s)
x = re.sub('[ ]+', ' ', x)

print(x)


Output:



""is this a test?"" Yes No 

""some text"" option1 option2 option3


Note: I had to somewhat fix the original text, i.e. add < before the first "suggested-action" and remove " at the end of the first element. Let me know if that's not OK and we need to fix this in code as well






share|improve this answer






























    1














    1.Read this complete text in your code using readlines() which would give you a list of rows.



    2.Using regex ,get text and other options in a list of lists.



    3.Load list of lists in to a dataframe.



    import re
    import pandas as pd
    df_list = []
    data = open('filename.txt','r').readlines()
    for row in data:
    m = re.search('=(.+?)>', text)
    text = m.group(1)
    row = re.sub('<.*?>','',row).split(' ')
    df_list.append([m,row[0],row[1],row[2])
    data_df = pd.Dataframe(df_list)





    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%2f55040908%2fpython-select-text-between-angle-brackets%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      s = """
      <suggested-actions-list text =""is this a test?""><suggested-action>Yes</suggested-action><suggested-action>No</suggested-action></suggested-actions-list>

      <choice-list text=""some text""> <choice-option>option1</choice-option><choice-option>option2</choice-option> <choice-option>option3</choice-option></choice-list>
      """

      x = re.sub('<(?:.*?)("".*"")?>', r'1 ', s)
      x = re.sub('[ ]+', ' ', x)

      print(x)


      Output:



      ""is this a test?"" Yes No 

      ""some text"" option1 option2 option3


      Note: I had to somewhat fix the original text, i.e. add < before the first "suggested-action" and remove " at the end of the first element. Let me know if that's not OK and we need to fix this in code as well






      share|improve this answer



























        1














        s = """
        <suggested-actions-list text =""is this a test?""><suggested-action>Yes</suggested-action><suggested-action>No</suggested-action></suggested-actions-list>

        <choice-list text=""some text""> <choice-option>option1</choice-option><choice-option>option2</choice-option> <choice-option>option3</choice-option></choice-list>
        """

        x = re.sub('<(?:.*?)("".*"")?>', r'1 ', s)
        x = re.sub('[ ]+', ' ', x)

        print(x)


        Output:



        ""is this a test?"" Yes No 

        ""some text"" option1 option2 option3


        Note: I had to somewhat fix the original text, i.e. add < before the first "suggested-action" and remove " at the end of the first element. Let me know if that's not OK and we need to fix this in code as well






        share|improve this answer

























          1












          1








          1







          s = """
          <suggested-actions-list text =""is this a test?""><suggested-action>Yes</suggested-action><suggested-action>No</suggested-action></suggested-actions-list>

          <choice-list text=""some text""> <choice-option>option1</choice-option><choice-option>option2</choice-option> <choice-option>option3</choice-option></choice-list>
          """

          x = re.sub('<(?:.*?)("".*"")?>', r'1 ', s)
          x = re.sub('[ ]+', ' ', x)

          print(x)


          Output:



          ""is this a test?"" Yes No 

          ""some text"" option1 option2 option3


          Note: I had to somewhat fix the original text, i.e. add < before the first "suggested-action" and remove " at the end of the first element. Let me know if that's not OK and we need to fix this in code as well






          share|improve this answer













          s = """
          <suggested-actions-list text =""is this a test?""><suggested-action>Yes</suggested-action><suggested-action>No</suggested-action></suggested-actions-list>

          <choice-list text=""some text""> <choice-option>option1</choice-option><choice-option>option2</choice-option> <choice-option>option3</choice-option></choice-list>
          """

          x = re.sub('<(?:.*?)("".*"")?>', r'1 ', s)
          x = re.sub('[ ]+', ' ', x)

          print(x)


          Output:



          ""is this a test?"" Yes No 

          ""some text"" option1 option2 option3


          Note: I had to somewhat fix the original text, i.e. add < before the first "suggested-action" and remove " at the end of the first element. Let me know if that's not OK and we need to fix this in code as well







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 10:39









          perlperl

          1,412312




          1,412312























              1














              1.Read this complete text in your code using readlines() which would give you a list of rows.



              2.Using regex ,get text and other options in a list of lists.



              3.Load list of lists in to a dataframe.



              import re
              import pandas as pd
              df_list = []
              data = open('filename.txt','r').readlines()
              for row in data:
              m = re.search('=(.+?)>', text)
              text = m.group(1)
              row = re.sub('<.*?>','',row).split(' ')
              df_list.append([m,row[0],row[1],row[2])
              data_df = pd.Dataframe(df_list)





              share|improve this answer



























                1














                1.Read this complete text in your code using readlines() which would give you a list of rows.



                2.Using regex ,get text and other options in a list of lists.



                3.Load list of lists in to a dataframe.



                import re
                import pandas as pd
                df_list = []
                data = open('filename.txt','r').readlines()
                for row in data:
                m = re.search('=(.+?)>', text)
                text = m.group(1)
                row = re.sub('<.*?>','',row).split(' ')
                df_list.append([m,row[0],row[1],row[2])
                data_df = pd.Dataframe(df_list)





                share|improve this answer

























                  1












                  1








                  1







                  1.Read this complete text in your code using readlines() which would give you a list of rows.



                  2.Using regex ,get text and other options in a list of lists.



                  3.Load list of lists in to a dataframe.



                  import re
                  import pandas as pd
                  df_list = []
                  data = open('filename.txt','r').readlines()
                  for row in data:
                  m = re.search('=(.+?)>', text)
                  text = m.group(1)
                  row = re.sub('<.*?>','',row).split(' ')
                  df_list.append([m,row[0],row[1],row[2])
                  data_df = pd.Dataframe(df_list)





                  share|improve this answer













                  1.Read this complete text in your code using readlines() which would give you a list of rows.



                  2.Using regex ,get text and other options in a list of lists.



                  3.Load list of lists in to a dataframe.



                  import re
                  import pandas as pd
                  df_list = []
                  data = open('filename.txt','r').readlines()
                  for row in data:
                  m = re.search('=(.+?)>', text)
                  text = m.group(1)
                  row = re.sub('<.*?>','',row).split(' ')
                  df_list.append([m,row[0],row[1],row[2])
                  data_df = pd.Dataframe(df_list)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 7 at 10:39









                  Yoshitha PenagantiYoshitha Penaganti

                  24614




                  24614



























                      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%2f55040908%2fpython-select-text-between-angle-brackets%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

                      AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                      Алба-Юлія

                      Захаров Федір Захарович