truncating strings

Discussion in 'Python' started by Roy Smith, Aug 23, 2011.

  1. Roy Smith

    Roy Smith Guest

    I want to log a string but only the first bunch of it, and add "..."
    to the end if it got truncated. This certainly works:

    log_message = message
    if len(log_message) >= 50:
    log_message = log_message[:50] + '...'
    logger.error("FAILED: '%s', '%s', %s, %s" % (log_message,
    route, params, e.code))

    but it bugs me that there should be some cleaner way to do this. I'm
    fantasizing about something along the lines of:

    logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
    route, params, e.code))

    does anything like this exist?
    Roy Smith, Aug 23, 2011
    #1
    1. Advertising

  2. Roy Smith

    Chris Rebert Guest

    On Tue, Aug 23, 2011 at 9:29 AM, Roy Smith <> wrote:
    > I want to log a string but only the first bunch of it, and add "..."
    > to the end if it got truncated.  This certainly works:
    >
    >          log_message = message
    >          if len(log_message) >= 50:
    >            log_message = log_message[:50]+ '...'
    >          logger.error("FAILED: '%s', '%s', %s, %s" % (log_message,
    > route, params, e.code))
    >
    > but it bugs me that there should be some cleaner way to do this.  I'm
    > fantasizing about something along the lines of:
    >
    >          logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
    > route, params, e.code))
    >
    > does anything like this exist?


    You can specify a maximum width to truncate the string to, but I don't
    see any built-in way to add an elision indication (e.g. "...").

    >>> "%.4s" % "spam and eggs"

    'spam'
    >>> "{:.4s}".format("spam and eggs")

    'spam'

    You could define something to wrap strings and override __format__()
    or similar, but that seems like overkill.

    Cheers,
    Chris
    --
    http://rebertia.com
    Chris Rebert, Aug 23, 2011
    #2
    1. Advertising

  3. Roy Smith

    Seebs Guest

    On 2011-08-23, Roy Smith <> wrote:
    > I want to log a string but only the first bunch of it, and add "..."
    > to the end if it got truncated. This certainly works:


    > logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
    > route, params, e.code))


    > does anything like this exist?


    %.50s

    -s
    --
    Copyright 2011, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Aug 23, 2011
    #3
  4. Roy Smith

    Seebs Guest

    On 2011-08-23, Ethan Furman <> wrote:
    > Seebs wrote:
    >> On 2011-08-23, Roy Smith <> wrote:
    >>> logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
    >>> route, params, e.code))


    >>> does anything like this exist?


    >> %.50s


    > That's not working in 2.7 or 3.2.


    Huh.

    Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print "%.5s" % ("hello there, truncate me!")

    hello

    -s
    --
    Copyright 2011, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Aug 23, 2011
    #4
  5. Roy Smith

    Ethan Furman Guest

    Seebs wrote:
    > On 2011-08-23, Roy Smith <> wrote:
    >> I want to log a string but only the first bunch of it, and add "..."
    >> to the end if it got truncated. This certainly works:

    >
    >> logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
    >> route, params, e.code))

    >
    >> does anything like this exist?

    >
    > %.50s


    That's not working in 2.7 or 3.2.

    ~Ethan~
    Ethan Furman, Aug 23, 2011
    #5
  6. Roy Smith

    Ethan Furman Guest

    Seebs wrote:
    > On 2011-08-23, Ethan Furman <> wrote:
    >> Seebs wrote:
    >>> On 2011-08-23, Roy Smith <> wrote:
    >>>> logger.error("FAILED: '%s{50}', '%s', %s, %s" % (message,
    >>>> route, params, e.code))

    >
    >>>> does anything like this exist?

    >
    >>> %.50s

    >
    >> That's not working in 2.7 or 3.2.

    >
    > Huh.
    >
    > Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
    > [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> print "%.5s" % ("hello there, truncate me!")
    > hello


    Ah -- that's only part of it -- the OP wants '...' to print as well. :)

    ~Ethan~
    Ethan Furman, Aug 24, 2011
    #6
  7. Seebs wrote:

    > Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
    > [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> print "%.5s" % ("hello there, truncate me!")

    > hello



    Well, whadda you know, I learned something new :)


    In any case, this doesn't solve the OP's problem, as he wants to truncate
    the input string, and append '...' if and only if it were truncated.

    The right solution is to wrap the functionality in a function. It's not
    hard, and is elegant. Not everything needs to be a built-in.

    # Untested.
    def truncate(s, maxwidth=50):
    if len(s) <= maxwidth:
    return s
    s = s[:maxwidth - 3]
    return s + '...'



    --
    Steven
    Steven D'Aprano, Aug 24, 2011
    #7
  8. Roy Smith

    Seebs Guest

    On 2011-08-23, Ethan Furman <> wrote:
    > Ah -- that's only part of it -- the OP wants '...' to print as well. :)


    Ohhhh. Hmm.

    That's harder. I can't think of a pretty way, so I think I'd probably
    write a "prettytrunc(string, len)" or something similar.

    -s
    --
    Copyright 2011, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Aug 24, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jitesh Sinha
    Replies:
    1
    Views:
    619
    Munsifali Rashid
    Dec 5, 2003
  2. Jason Williard

    Truncating Variables

    Jason Williard, Oct 15, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    5,334
    Kevin Spencer
    Oct 18, 2004
  3. uy_do
    Replies:
    1
    Views:
    664
    Harish Madhavan
    Dec 4, 2003
  4. BemusedByQM

    truncating java 'doubles'

    BemusedByQM, Jul 24, 2005, in forum: Java
    Replies:
    2
    Views:
    17,406
    jfalt
    Jul 25, 2005
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    739
    Malcolm
    Jun 24, 2006
Loading...

Share This Page