469,613 Members | 1,294 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

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.pointer_file ):
ptr_file = open( group_info.pointer_file, "r" )
else:
print group_info.mess_list
return
linecount = 0
ptr_file.seek( 512 )
print ptr_file.tell()
line = ptr_file.readline()
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.readline()
ptr_file.close()

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

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

instead of
['<Jo***************@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.group_name+" sf"+ group_info.first + " sl"+ \ group_info.last + " sc" + group_info.count + "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.
Jun 14 '06 #1
3 1380
Sambo wrote:
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??


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.

Jun 15 '06 #2
Serge Orlov wrote:
Sambo wrote:
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??


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.


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

George

Jun 15 '06 #3
George Sakkis wrote:
Serge Orlov wrote:

Sambo wrote:
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??


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.

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

George

AHA! Thank You.
Jun 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Stu Cazzo | last post: by
1 post views Thread by Sam Johnson | last post: by
7 posts views Thread by Johny | last post: by
5 posts views Thread by shaiful | last post: by
7 posts views Thread by spoken | last post: by
4 posts views Thread by N9 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.