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 454431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:16:12+00:00 2026-05-12T22:16:12+00:00

I’m looking for a way to automatically produce an abstract, basically the first few

  • 0

I’m looking for a way to automatically produce an abstract, basically the first few sentances/paragraphs of a blog entry, to display in a list of articles (which are written in markdown). Currently, I’m doing something like this:

def abstract(article, paras=3):
    return '\n'.join(article.split('\n')[0:paras])

to just grab the first few lines worth of text, but i’m not totally happy with the results.

What I’m really looking for is to end up with about 1/3 of a screenful of formatted text to display in the list of entries, but using the algorithm above, the amount pulled ends up with wildly varying amounts, as little as a line or two, is frequently mixed with more ideal sized abstracts.

Is there a library that’s good at this kind of thing? if not, do you have any suggestions to improve the output?

  • 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-05-12T22:16:13+00:00Added an answer on May 12, 2026 at 10:16 pm

    EDIT:

    You can do something like this:

    from textwrap import wrap
    
    def getAbstract(text, lines=5, screenwidth=100):
        width = len(' '.join([
                   line for block in text.splitlines()
                   for line in wrap(block, width=screenwidth)
                ][:lines]))
        return text[:width] + '...'
    

    This makes use of the textwrap algorithm to get the ideal text length. It will break the text into screen-sized lines and use them to calculate the length of the desirable number of lines.

    For example applying this algorithm on the python wikipedia page entry:

    print getAbstract(text, lines=7)
    

    will give you this output:

    Python is a general-purpose high-level
    programming language.2 Its design
    philosophy emphasizes code
    readability.[3] Python claims to
    “[combine] remarkable power with very
    clear syntax”,[4] and its standard
    library is large and comprehensive.
    Its use of indentation as block
    delimiters is unusual among popular
    programming languages.

    Python supports multiple programming
    paradigms (primarily object oriented,
    imperative, and functional) and
    features a fully dynamic type system
    and automatic memory management,
    similar to Perl, Ruby, Scheme, and
    Tcl. Like other dynamic languages,
    Python is often used as a scripting…


    Without further details it’s hard to help you. But if your problem was that taking the first few lines was too much for some entries you may need to have a look at textwrap

    For example if you only want 100 character abstracts you can do the following:

    import textwrap
    
    abstract = textwrap.wrap(text, 100)[0]
    

    That will also replace newlines with spaces which might be desirable depending on your requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
i want to parse a xhtml file and display in UITableView. what is the
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.