split with "*" in string and ljust() puzzles

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

    #1

    split with "*" in string and ljust() puzzles

    I have couple of puzzles in my code.

    def load_headers( group_info ):

    if os.path.isfile( group_info.poin ter_file ):
    ptr_file = open( group_info.poin ter_file, "r" )
    else:
    print group_info.mess _list
    return
    linecount = 0
    ptr_file.seek( 512 )
    print ptr_file.tell()
    line = ptr_file.readli ne()
    while line != "" :
    if line[0:1] == "<":
    print linecount
    print len(line), line
    print line.split( " ", 3 )

    group_info.mess _list.append( line.split( " ", 3 ) )
    line = ptr_file.readli ne()
    ptr_file.close( )

    when reading the following line from file:
    <Jodgg.1478$uP. 205@newsfe2-gui.ntli.net> 2338 * Re: PCB Pad Size for TQFP Package??

    the split command returns
    ['<Jodgg.1478$uP .205@newsfe2-gui.ntli.net> ','','','2338 * Re: PCB Pad Size for TQFP Package??']

    instead of
    ['<Jodgg.1478$uP .205@newsfe2-gui.ntli.net>, '2338', '*','Re: PCB Pad Size for TQFP Package??']



    I have just (finally) realized that it is splitting and removing on single space but that seams useless, and split items 1 and 2 are empty strings not spaces?? regex somewhere it shouldn't be?



    The other problem
    is in this piece of code which is trying to pad the first 512 bytes:

    line = group_info.grou p_name+" sf"+ group_info.firs t + " sl"+ \ group_info.last + " sc" + group_info.coun t + "dt" + \ group_info.date _checked + group_info.time _checked
    line = line + "\n"
    line = string.ljust( line, 512 - len(os.linesep) )
    print len( os.linesep )
    line += "\n"
    print len( line )
    ptr_file.write( line )
    print "**** "+repr(ptr_file .tell())+ " ****"
    print "message list\n"

    the ljust function returns string 511 bytes long, besides the fact that the logic is not exactly correct what is going on here. Is ljust trying to be smart about the EOL inside the string already?

    I have tried the following, which should be about right ( account for the 1 bytes added after justification and two added by file write.)

    line = string.ljust( line, 512 - len(os.linesep) - len(os.linesep) - 1 )

    But, in this case I end up 2 bytes short of 512.

    Thanks for any help.
  • Serge Orlov

    #2
    Re: split with &quot;*&quot ; in string and ljust() puzzles

    Sambo wrote:[color=blue]
    > I have just (finally) realized that it is splitting and removing
    > on single space but that seams useless, and split items
    > 1 and 2 are empty strings not spaces??[/color]

    What is useless for you is worth $1,000,000 for somebody else ;)
    If you have comma separated list '1,,2'.split(', ') naturally returns
    ['1', '', '2']. I think you can get what you want with a simple regexp.

    Comment

    • George Sakkis

      #3
      Re: split with &quot;*&quot ; in string and ljust() puzzles

      Serge Orlov wrote:
      [color=blue]
      > Sambo wrote:[color=green]
      > > I have just (finally) realized that it is splitting and removing
      > > on single space but that seams useless, and split items
      > > 1 and 2 are empty strings not spaces??[/color]
      >
      > What is useless for you is worth $1,000,000 for somebody else ;)
      > If you have comma separated list '1,,2'.split(', ') naturally returns
      > ['1', '', '2']. I think you can get what you want with a simple regexp.[/color]

      No need for regexp in this case, just use None to specify one or more
      whitespace chars as delimiter: line.split(None ,3)

      George

      Comment

      • Sambo

        #4
        Re: split with &quot;*&quot ; in string and ljust() puzzles

        George Sakkis wrote:[color=blue]
        > Serge Orlov wrote:
        >
        >[color=green]
        >>Sambo wrote:
        >>[color=darkred]
        >>>I have just (finally) realized that it is splitting and removing
        >>>on single space but that seams useless, and split items
        >>>1 and 2 are empty strings not spaces??[/color]
        >>
        >>What is useless for you is worth $1,000,000 for somebody else ;)
        >>If you have comma separated list '1,,2'.split(', ') naturally returns
        >>['1', '', '2']. I think you can get what you want with a simple regexp.[/color]
        >
        >
        > No need for regexp in this case, just use None to specify one or more
        > whitespace chars as delimiter: line.split(None ,3)
        >
        > George
        >[/color]
        AHA! Thank You.

        Comment

        Working...