line wrapping problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • S Borg

    #1

    line wrapping problem

    Hello,

    I am parsing text from one document to another. I have a scheme
    similar to:

    for x in myfoobar:
    print >> mytextfile, "%s " % mydictionary[x], #all on same line
    print >> mytextfile, '\n' #new line


    I am getting line breaks before my explicit line break. Am I
    unwittingly copying '\n' characters from the original file?
    How should I fix this(What is the 'Pythonic' solution)?

    thanks,
    -S

  • Fuzzyman

    #2
    Re: line wrapping problem


    S Borg wrote:[color=blue]
    > Hello,
    >
    > I am parsing text from one document to another. I have a scheme
    > similar to:
    >
    > for x in myfoobar:
    > print >> mytextfile, "%s " % mydictionary[x], #all on same line
    > print >> mytextfile, '\n' #new line
    >[/color]

    You are using the print command to output the text to your file. This
    will always put a '\n' at the end of lines (as well as your explicit)
    one. The only time it doesn't do this is if the line ends with a
    trailing comma, in which case it just adds a space.

    The usual way is to write to yhour file object using :

    mytextfile.writ e( mytextfile, "%s " % mydictionary[x],)

    All the best,

    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml
    [color=blue]
    >
    > I am getting line breaks before my explicit line break. Am I
    > unwittingly copying '\n' characters from the original file?
    > How should I fix this(What is the 'Pythonic' solution)?
    >
    > thanks,
    > -S[/color]

    Comment

    • Juho Schultz

      #3
      Re: line wrapping problem

      S Borg wrote:[color=blue]
      > Hello,
      >
      > I am parsing text from one document to another. I have a scheme
      > similar to:
      >
      > for x in myfoobar:
      > print >> mytextfile, "%s " % mydictionary[x], #all on same line
      > print >> mytextfile, '\n' #new line
      >
      >
      > I am getting line breaks before my explicit line break. Am I
      > unwittingly copying '\n' characters from the original file?
      > How should I fix this(What is the 'Pythonic' solution)?
      >
      > thanks,
      > -S
      >[/color]

      mytextfile.writ e("%s " % mydictionary[x])

      should work. IMO file.write() is self-explanatory but "print >> file" is
      a bit obscure.

      Comment

      • John Zenger

        #4
        Re: line wrapping problem

        S Borg wrote:
        [color=blue]
        > print >> mytextfile, '\n' #new line[/color]

        Wouldn't this print two line breaks--the one you specify in the string,
        plus the one always added by print if there is no comma at the end of
        the statement?

        Comment

        • ZeD

          #5
          Re: line wrapping problem

          Ciao, Juho Schultz! Che stavi dicendo?
          [color=blue]
          > should work. IMO file.write() is self-explanatory but "print >> file" is
          > a bit obscure.[/color]

          is obscure only if you have never used a shell :)

          --
          Evangelion e' la storia yaoi di un angelo che vuole portarsi a letto un
          ragazzo che si intreccia con la storia di un maturo professionista con il
          complesso delle lolite... fatte in casa.
          -- Lennier, 2006

          Comment

          • Peter Otten

            #6
            Re: line wrapping problem

            S Borg wrote:
            [color=blue]
            > I am parsing text from one document to another. I have a scheme
            > similar to:
            >
            > for x in myfoobar:
            > print >> mytextfile, "%s " % mydictionary[x], #all on same line[/color]

            print >> mytextfile # minimal fix
            [color=blue]
            > I am getting line breaks before my explicit line break. Am I
            > unwittingly copying '\n' characters from the original file?
            > How should I fix this(What is the 'Pythonic' solution)?[/color]

            The trailing spaces are probably an artifact of your implementation. Here's
            one way to avoid them:

            print >> mytextfile, " ".join(str(mydi ctionary[x]) for x in myfoobar)

            Peter

            Comment

            • Juho Schultz

              #7
              Re: line wrapping problem

              ZeD wrote:[color=blue]
              > Ciao, Juho Schultz! Che stavi dicendo?
              >[/color]
              Moro, ZeD! Kunhan pulisen. Should we stick to English?[color=blue]
              >[color=green]
              >>should work. IMO file.write() is self-explanatory but "print >> file" is
              >>a bit obscure.[/color]
              >
              > is obscure only if you have never used a shell :)
              >[/color]
              (I have used the shell a bit. I started using Linux at work when 2.2
              series kernels did not exist.)

              Assume a newbie plays around with a piece code. If the code has
              f.write(x) or print >> f,x - in which case the newbie is more likely to
              break the code by rebinding f to something non-file? And in which case
              he will more likely understand the error message, something like "f has
              no write attribute"?

              I am sure the >> can be useful - it is quick to add after a simple print
              when you want less chatter and more logfiles. But IMO it is not
              self-documenting. Having to very different uses for an operator in the
              same language is sort of confusing.

              Comment

              • Kent Johnson

                #8
                Re: line wrapping problem

                S Borg wrote:[color=blue]
                > Hello,
                >
                > I am parsing text from one document to another. I have a scheme
                > similar to:
                >
                > for x in myfoobar:
                > print >> mytextfile, "%s " % mydictionary[x], #all on same line
                > print >> mytextfile, '\n' #new line
                >
                >
                > I am getting line breaks before my explicit line break. Am I
                > unwittingly copying '\n' characters from the original file?[/color]

                Where does mydictionary[x] come from? If it is from reading lines from a
                file it may contain trailing newlines.

                Also
                print >> mytextfile, '\n'
                will print two newlines, one explicit and one implicit in the print.

                Kent

                Comment

                Working...