Stop spaces from appearing when printing to file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brad Tilley

    #1

    Stop spaces from appearing when printing to file

    I'm printing some info into a txt file that will be uploaded into a
    MySQL DB. I use ';' as field separaters. How can I stop spaces from
    appearing on both sides of the ';'

    When I do this:

    print >> x,';',object,"; ",AN_string,";" ,ascii,";",sum

    My file looks like this:

    ;12345 ; 23456 ; [1,2,3,4,5] ; 15

    I need it to look like this to make the DB happy:

    ;12345;23456;[1,2,3,4,5];15
  • Kent Johnson

    #2
    Re: Stop spaces from appearing when printing to file

    Brad Tilley wrote:[color=blue]
    > I'm printing some info into a txt file that will be uploaded into a
    > MySQL DB. I use ';' as field separaters. How can I stop spaces from
    > appearing on both sides of the ';'[/color]

    The spaces are a feature of print. To avoid them, use string formatting
    to create a single output string.
    [color=blue]
    > print >> x,';',object,"; ",AN_string,";" ,ascii,";",sum[/color]

    Try
    print >> x, ';%s;%s;%s;%s' % (object, AN_string, ascii, sum)

    Kent
    [color=blue]
    >
    > My file looks like this:
    >
    > ;12345 ; 23456 ; [1,2,3,4,5] ; 15
    >
    > I need it to look like this to make the DB happy:
    >
    > ;12345;23456;[1,2,3,4,5];15[/color]

    Comment

    • news.west.cox.net

      #3
      Re: Stop spaces from appearing when printing to file

      > The spaces are a feature of print. To avoid them, use string formatting to[color=blue]
      > create a single output string.
      >[color=green]
      >> print >> x,';',object,"; ",AN_string,";" ,ascii,";",sum[/color]
      >
      > Try
      > print >> x, ';%s;%s;%s;%s' % (object, AN_string, ascii, sum)
      >
      > Kent
      >[/color]
      or....

      print x + ';' + object + ";" + AN_string + ";" + ascii + ";" + sum


      Comment

      • Dave Reed

        #4
        Re: Stop spaces from appearing when printing to file

        On Sunday 07 November 2004 20:02, Brad Tilley wrote:[color=blue]
        > I'm printing some info into a txt file that will be uploaded into a
        > MySQL DB. I use ';' as field separaters. How can I stop spaces from
        > appearing on both sides of the ';'
        >
        > When I do this:
        >
        > print >> x,';',object,"; ",AN_string,";" ,ascii,";",sum
        >
        > My file looks like this:
        >
        > ;12345 ; 23456 ; [1,2,3,4,5] ; 15
        >
        > I need it to look like this to make the DB happy:
        >
        > ;12345;23456;[1,2,3,4,5];15[/color]


        Instead of using print, open a file and use the "write" method of the
        file object. See the documentation for file objects for more detail.

        Dave


        Comment

        • Brad Tilley

          #5
          Re: Stop spaces from appearing when printing to file

          Kent Johnson wrote:[color=blue]
          > Brad Tilley wrote:
          >[color=green]
          >> I'm printing some info into a txt file that will be uploaded into a
          >> MySQL DB. I use ';' as field separaters. How can I stop spaces from
          >> appearing on both sides of the ';'[/color]
          >
          >
          > The spaces are a feature of print. To avoid them, use string formatting
          > to create a single output string.
          >[color=green]
          >> print >> x,';',object,"; ",AN_string,";" ,ascii,";",sum[/color]
          >
          >
          > Try
          > print >> x, ';%s;%s;%s;%s' % (object, AN_string, ascii, sum)
          >
          > Kent[/color]

          Thanks Kent, this works great.

          Comment

          • Scott David Daniels

            #6
            Re: Stop spaces from appearing when printing to file

            Kent Johnson wrote:[color=blue]
            > Brad Tilley wrote:
            >[color=green]
            >> I'm printing some info into a txt file that will be uploaded into a
            >> MySQL DB. I use ';' as field separaters. How can I stop spaces from
            >> appearing on both sides of the ';'[/color]
            >
            >
            > The spaces are a feature of print. To avoid them, use string formatting
            > to create a single output string.
            >[color=green]
            >> print >> x,';',object,"; ",AN_string,";" ,ascii,";",sum[/color]
            >
            >
            > Try
            > print >> x, ';%s;%s;%s;%s' % (object, AN_string, ascii, sum)[/color]

            or even:
            print >>x, ';'.join([str(v) for v in (object, AN_string, ascii, sum)])

            -Scott David Daniels
            Scott.DanielsAc m.Org

            Comment

            • Terry Hancock

              #7
              Re: Stop spaces from appearing when printing to file

              On Sunday 07 November 2004 07:02 pm, Brad Tilley wrote:[color=blue]
              > I'm printing some info into a txt file that will be uploaded into a
              > MySQL DB. I use ';' as field separaters. How can I stop spaces from
              > appearing on both sides of the ';'
              >
              > When I do this:
              >
              > print >> x,';',object,"; ",AN_string,";" ,ascii,";",sum
              >
              > My file looks like this:
              >
              > ;12345 ; 23456 ; [1,2,3,4,5] ; 15
              >
              > I need it to look like this to make the DB happy:
              >
              > ;12345;23456;[1,2,3,4,5];15[/color]

              Generally speaking, you'll be better off to produce the string
              you want and then print it, rather than relying on the print
              command's syntax. You'll find it much more intuitive to do
              either:

              print x+';'+object+"; "+AN_string+";" +ascii+";"+sum

              or

              print "%s;%s;%s;%s;%s " % (x, object, AN_string, ascii, sum)

              In other words, use string operators, then print the result.

              Cheers,
              Terry

              --
              Terry Hancock ( hancock at anansispacework s.com )
              Anansi Spaceworks http://www.anansispaceworks.com

              Comment

              Working...