repr(string)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David C. Ullrich

    repr(string)

    I've been saving data in a file with one line per field.
    Now some of the fields may become multi-line strings...

    I was about to start escaping and unescaping linefeeds
    by hand, when I realized that repr() and eval() should
    do. Hence the question: If s is a string, is repr(s)
    guaranteed not to contain line breaks?

    --
    David C. Ullrich
  • Guilherme Polo

    #2
    Re: repr(string)

    On Wed, Jul 23, 2008 at 12:04 PM, David C. Ullrich <dullrich@spryn et.comwrote:
    I've been saving data in a file with one line per field.
    Now some of the fields may become multi-line strings...
    >
    I was about to start escaping and unescaping linefeeds
    by hand, when I realized that repr() and eval() should
    do. Hence the question: If s is a string, is repr(s)
    guaranteed not to contain line breaks?
    In the sense that line breaks are quoted ? Yes, repr does that.


    --
    -- Guilherme H. Polo Goncalves

    Comment

    • Fredrik Lundh

      #3
      Re: repr(string)

      David C. Ullrich wrote:
      I've been saving data in a file with one line per field.
      Now some of the fields may become multi-line strings...
      >
      I was about to start escaping and unescaping linefeeds
      by hand, when I realized that repr() and eval() should
      do. Hence the question: If s is a string, is repr(s)
      guaranteed not to contain line breaks?
      yes.

      just keep in mind that using eval() on untrusted data isn't a very good
      idea.

      </F>

      Comment

      • David C. Ullrich

        #4
        Re: repr(string)

        In article <mailman.561.12 16843813.922.py thon-list@python.org >,
        Fredrik Lundh <fredrik@python ware.comwrote:
        David C. Ullrich wrote:
        >
        I've been saving data in a file with one line per field.
        Now some of the fields may become multi-line strings...

        I was about to start escaping and unescaping linefeeds
        by hand, when I realized that repr() and eval() should
        do. Hence the question: If s is a string, is repr(s)
        guaranteed not to contain line breaks?
        >
        yes.
        >
        just keep in mind that using eval() on untrusted data isn't a very good
        idea.
        Right. This data comes from me, gets put into a file and then
        read by me. Someone _could_ corrupt that file, but someone who
        could do that could more easily just throw the machine out
        the window...
        </F>
        --
        David C. Ullrich

        Comment

        • Peter Otten

          #5
          Re: repr(string)

          David C. Ullrich wrote:
          In article <mailman.561.12 16843813.922.py thon-list@python.org >,
          Fredrik Lundh <fredrik@python ware.comwrote:
          >
          >David C. Ullrich wrote:
          >>
          I've been saving data in a file with one line per field.
          Now some of the fields may become multi-line strings...
          >
          I was about to start escaping and unescaping linefeeds
          by hand, when I realized that repr() and eval() should
          do. Hence the question: If s is a string, is repr(s)
          guaranteed not to contain line breaks?
          >>
          >yes.
          >>
          >just keep in mind that using eval() on untrusted data isn't a very good
          >idea.
          >
          Right. This data comes from me, gets put into a file and then
          read by me. Someone _could_ corrupt that file, but someone who
          could do that could more easily just throw the machine out
          the window...
          You could also use a csv file with a single row.

          Peter

          Comment

          • Peter Otten

            #6
            Re: repr(string)

            Peter Otten wrote:
            You could also use a csv file with a single row.
            Err, I meant column, but a row would also work. Your choice.

            Peter

            Comment

            • Fredrik Lundh

              #7
              Re: repr(string)

              David C. Ullrich skrev:
              >just keep in mind that using eval() on untrusted data isn't a very good
              >idea.
              >
              Right. This data comes from me, gets put into a file and then
              read by me. Someone _could_ corrupt that file, but someone who
              could do that could more easily just throw the machine out
              the window...
              and then your boss finds your program useful, and it's installed on a
              shared server, and then the guys at the office in Eggkleiva wants a
              copy, and then people start shipping save files via mail to keep things
              synchronized, and then someone sets up a web service... ;-)

              </F>

              Comment

              • David C. Ullrich

                #8
                Re: repr(string)

                In article <mailman.602.12 16913758.922.py thon-list@python.org >,
                Fredrik Lundh <fredrik@python ware.comwrote:
                David C. Ullrich skrev:
                >
                just keep in mind that using eval() on untrusted data isn't a very good
                idea.
                Right. This data comes from me, gets put into a file and then
                read by me. Someone _could_ corrupt that file, but someone who
                could do that could more easily just throw the machine out
                the window...
                >
                and then your boss finds your program useful, and it's installed on a
                shared server, and then the guys at the office in Eggkleiva wants a
                copy, and then people start shipping save files via mail to keep things
                synchronized, and then someone sets up a web service... ;-)
                Heh-heh. Good point, except that the idea that someone's going to
                find it useful is utterly implausible. Nobody but me has ever found
                a program I wrote useful. People think it's funny that I write little
                Python programs to do things I could just do in Excel or Open
                Office. (When I have some accounting/secretarial sort of thing
                to do doing it by hand in Python is one way to make it tolerably
                interesting. Easier to add new features - instead of trying to find
                an Excel way to do something like delete the smallest _two_
                items in a list I just do it.)
                </F>
                --
                David C. Ullrich

                Comment

                • David C. Ullrich

                  #9
                  Re: repr(string)

                  In article <g6a6u2$kfk$03$ 1@news.t-online.com>,
                  Peter Otten <__peter__@web. dewrote:
                  David C. Ullrich wrote:
                  >
                  In article <mailman.561.12 16843813.922.py thon-list@python.org >,
                  Fredrik Lundh <fredrik@python ware.comwrote:
                  David C. Ullrich wrote:
                  >
                  I've been saving data in a file with one line per field.
                  Now some of the fields may become multi-line strings...

                  I was about to start escaping and unescaping linefeeds
                  by hand, when I realized that repr() and eval() should
                  do. Hence the question: If s is a string, is repr(s)
                  guaranteed not to contain line breaks?
                  >
                  yes.
                  >
                  just keep in mind that using eval() on untrusted data isn't a very good
                  idea.
                  Right. This data comes from me, gets put into a file and then
                  read by me. Someone _could_ corrupt that file, but someone who
                  could do that could more easily just throw the machine out
                  the window...
                  >
                  You could also use a csv file with a single row.
                  Excellent suggestion. I have a different point of view on all
                  this than most of you guys (see reply to F). From my curious
                  point of view csv was no fun anymore when csvlib got added to Python...
                  Peter
                  --
                  David C. Ullrich

                  Comment

                  • MRAB

                    #10
                    Re: repr(string)

                    On Jul 23, 4:04 pm, "David C. Ullrich" <dullr...@spryn et.comwrote:
                    I've been saving data in a file with one line per field.
                    Now some of the fields may become multi-line strings...
                    >
                    I was about to start escaping and unescaping linefeeds
                    by hand, when I realized that repr() and eval() should
                    do. Hence the question: If s is a string, is repr(s)
                    guaranteed not to contain line breaks?
                    >
                    Might I suggest you use encode and decode instead?
                    >>'first line\nsecond line'.encode('s tring-escape')
                    'first line\\nsecond line'
                    >>_.decode('str ing-escape')
                    'first line\nsecond line'
                    >>u'first line\nsecond line'.encode('u nicode-escape')
                    'first line\\nsecond line'
                    >>_.decode('uni code-escape')
                    u'first line\nsecond line'

                    Comment

                    • David C. Ullrich

                      #11
                      Re: repr(string)

                      In article
                      <c55ff9a0-abb9-48a3-990c-c643d61b8641@a6 g2000prm.google groups.com>,
                      MRAB <google@mrabarn ett.plus.comwro te:
                      On Jul 23, 4:04 pm, "David C. Ullrich" <dullr...@spryn et.comwrote:
                      I've been saving data in a file with one line per field.
                      Now some of the fields may become multi-line strings...

                      I was about to start escaping and unescaping linefeeds
                      by hand, when I realized that repr() and eval() should
                      do. Hence the question: If s is a string, is repr(s)
                      guaranteed not to contain line breaks?
                      Might I suggest you use encode and decode instead?
                      Ah, you certainly might! Seems exactly right, thanks.

                      Many years ago I thought I had some idea of what was available
                      in Python - these days it's full of all this neat stuff I never
                      heard of...
                      >'first line\nsecond line'.encode('s tring-escape')
                      'first line\\nsecond line'
                      >_.decode('stri ng-escape')
                      'first line\nsecond line'
                      >u'first line\nsecond line'.encode('u nicode-escape')
                      'first line\\nsecond line'
                      >_.decode('unic ode-escape')
                      u'first line\nsecond line'
                      --
                      David C. Ullrich

                      Comment

                      Working...