Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8610975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:15:24+00:00 2026-06-12T04:15:24+00:00

Possible Duplicate: Numbering the sentences inside a <P> in a .xml file? I’m just

  • 0

Possible Duplicate:
Numbering the sentences inside a <P> in a .xml file?

I’m just starting out in programming, so this problem is very trivial, except for me. I have a .xml file containing content like:

<p> sentence1. sentence2. sentence3.</p>
<p> sentence1. </p>

Now I have written a script with BeautifulSoup to append each single paragraph ending with a STRING, so it looks like:

<p> sentence1. sentence2. sentence3. STRING</p>
<p> sentence1. STRING </p>

In the < p > that contain only 1 sentence that is all I want to do. But if a < p > contains more than I sentence, I want to add the STRING to each sentence ending + the sentence number. For example the upper paragraph would be:

<p> sentence1. STRING1 sentence2. STRING2 sentence3. STRING3 </p>

Here is my working script for 1 sentence with the .append method, but I couldn’t get it to work for multiple sentences. Any help would be appreciated!

soup = BeautifulSoup(xmlfile)
p = norm.findAll("p")

for i in p:
    dotsplit = re.compile(r'\. \w')
    sentences = dotsplit.split(i.text)

    if len(sentences) == 1:
        appendix = "STRING"
        i.append(appendix)
        print i

    if len(sentences) > 1:
        for x in sentences:
            sentencenumber = ???????  
            # Should equal (index of sentences)+1,  meaning sentences[0] = 1
            appendix = sentencenumber + "STRING"
            i.append(appendix)
            print i
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:15:25+00:00Added an answer on June 12, 2026 at 4:15 am

    If I understand you correctly:

    if len(sentences) == 1:
        print sentences[0] + 'STRING'
    elif len(sentences) > 1:
        isentences = ('%s%s%d' % (s, 'STRING', i) for i, s in enumerate(sentences, 1))
        print ' '.join(isentences)
    

    I dont know how to append it after each sentence

    BeautifulSoup documentation say that you must use method tag.string.replace_with instead of tag.append:

        isentences = ('%s%s%d' % (s, 'STRING', i) for i, s in enumerate(sentences, 1))
        i.string.replace_with(' '.join(isentences))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Need help in structuring things in XML file have a structure like
Possible Duplicate: Parsing XML String in C# How to process the below XML file
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: C#: How to reference generic classes and methods in xml documentation I
Possible Duplicate: std::string and its automatic memory resizing I am just curious, how are
Possible Duplicate: how to delete a file? My application first reads the fields in
Possible Duplicate: What is the fastest XML parser in PHP? I still need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.