Formated String in optparse

Discussion in 'Python' started by Norbert Thek, Mar 31, 2005.

  1. Norbert Thek

    Norbert Thek Guest

    Hi

    I'm using Python 24 on Windows > (2k)

    Is there an easy way to convince optparse to accept newline in the helpstring?
    and more importand also in the 'desc' string. I tried everything
    (from the os.linesep) to \n, \r, \r\n, ...

    Norbert
    Norbert Thek, Mar 31, 2005
    #1
    1. Advertising

  2. Norbert Thek

    Peter Otten Guest

    Norbert Thek wrote:

    > Is there an easy way to convince optparse to accept newline in the
    > helpstring? and more importand also in the 'desc' string. I tried
    > everything
    > (from the os.linesep) to \n, \r, \r\n, ...


    The "official" way (write your own Formatter class) is a bit tedious indeed.
    Here's a hack that seems to work:

    import textwrap
    import optparse

    class TextWrapper:
    @staticmethod
    def wrap(text, width=70, **kw):
    result = []
    for line in text.split("\n"):
    result.extend(textwrap.wrap(line, width, **kw))
    return result
    @staticmethod
    def fill(text, width=70, **kw):
    result = []
    for line in text.split("\n"):
    result.append(textwrap.fill(line, width, **kw))
    return "\n".join(result)


    optparse.textwrap = TextWrapper()

    parser = optparse.OptionParser(description="""\
    einsamer nie als im august
    erfuellungsstunde im gelaende
    die roten und die goldenen braende
    doch wo ist deiner gaerten lust
    """)

    parser.add_option("-x", "--example", help="""\
    die seen hell die himmel weich
    die aecker rein und glaenzen leise
    doch wo sind sieg und siegsbeweise \
    aus dem von dir vertretenen reich \
    wo alles sich durch glueck""")

    parser.print_help()

    You should probably replace the "\n" literals with os.linesep.

    Peter
    Peter Otten, Mar 31, 2005
    #2
    1. Advertising

  3. Norbert Thek

    MyHaz Guest

    If you haven't looked into it, you may like the way class
    OptionParser() makes the help text for you.


    - Haz
    MyHaz, Mar 31, 2005
    #3
  4. Norbert Thek

    Peter Otten Guest

    MyHaz wrote:

    > If you haven't looked into it, you may like the way class
    > OptionParser() makes the help text for you.


    What do you mean?

    To clarify: OptionParser's help message in the default format is

    """
    usage: discard_newline.py [options]

    einsamer nie als im august erfuellungsstunde im gelaende die roten und die
    goldenen braende doch wo ist deiner gaerten lust

    options:
    -h, --help show this help message and exit
    -x EXAMPLE, --example=EXAMPLE
    die seen hell die himmel weich die aecker rein und
    glaenzen leise doch wo sind sieg und siegsbeweise
    aus
    dem von dir vertretenen reich wo alles sich durch
    glueck
    """


    whereas that same OptionParser with the tweaked optparse.textwrap (my
    TextWrapper instance replacing the textwrap module from the standard
    library) will output

    """
    usage: preserve_newline.py [options]

    einsamer nie als im august
    erfuellungsstunde im gelaende
    die roten und die goldenen braende
    doch wo ist deiner gaerten lust


    options:
    -h, --help show this help message and exit
    -x EXAMPLE, --example=EXAMPLE
    die seen hell die himmel weich
    die aecker rein und glaenzen leise
    doch wo sind sieg und siegsbeweise aus dem von dir
    vertretenen reich wo alles sich durch glueck
    """

    I guess that both poets in residence and limmerickistas will prefer the
    latter form :)

    Peter
    Peter Otten, Mar 31, 2005
    #4
  5. Norbert Thek

    Norbert Thek Guest

    Thank You for your help, its working!

    Now I have an additional question.
    The problem is the encoding of the Text
    I'm using German, Can you tell me how to encode
    the textstring that the Windows commandline shows the special letters
    right?
    For exampel i get 'f³r' but i want 'für' (maybe reader with only an english
    enabled browser wouldn't see a difference..)

    I tried to work with the encode method of string but It didn't work for me
    some hint what to do?


    Norbert
    Norbert Thek, Apr 14, 2005
    #5
  6. Norbert Thek

    Peter Otten Guest

    Norbert Thek wrote:

    > Thank You for your help, its working!
    >
    > Now I have an additional question.


    ....which would warrant a separate thread...

    > The problem is the encoding of the Text
    > I'm using German, Can you tell me how to encode
    > the textstring that the Windows commandline shows the special letters
    > right?
    > For exampel i get 'f³r' but i want 'für' (maybe reader with only an
    > english enabled browser wouldn't see a difference..)
    >
    > I tried to work with the encode method of string but It didn't work for me
    > some hint what to do?


    >>> u"f³r" # what you have (minus unicode)

    u'f\xb3r'

    >>> u"für"

    u'f\xfcr' # what you need

    One way to get that (wrong) representation:

    >>> u"für".encode("cp1252").decode("cp850")

    u'f\xb3r'

    So it could be that you are interpreting cp1252 ("German" windows) as cp850
    ("German" DOS).

    Try the following script:

    # -*- coding: cp1252 -*-
    text = u"text with umlauts äöü ÄÖÜ ß".encode("cp850")
    print text


    If that works correctly, you can prepare every string literal in your
    program in the same way. Or redirect sys.stdout

    sys.stdout = codecs.getwriter("cp850")(sys.stdout)

    as posted by Martin von Löwis on de.comp.lang.python only two days ago.

    Peter
    Peter Otten, Apr 15, 2005
    #6
    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. Alejandro Penate-Diaz

    string to formated datetime

    Alejandro Penate-Diaz, Mar 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    367
    William F. Robertson, Jr.
    Mar 11, 2005
  2. Brett
    Replies:
    1
    Views:
    517
    Peter Otten
    Aug 14, 2004
  3. =?Utf-8?B?ZGF2aWQ=?=

    Can we populate a XML formated string into a dataset?

    =?Utf-8?B?ZGF2aWQ=?=, Mar 29, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    447
    =?Utf-8?B?ZGF2aWQ=?=
    Mar 30, 2007
  4. =?Utf-8?B?R3JlZyBH?=

    Convert currency formated string back into decimal

    =?Utf-8?B?R3JlZyBH?=, Nov 3, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    905
    Martin Honnen
    Nov 3, 2007
  5. walterbyrd
    Replies:
    13
    Views:
    1,294
    walterbyrd
    May 13, 2009
Loading...

Share This Page