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

S

Sambo

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:
<[email protected]> 2338 * Re: PCB Pad Size for TQFP Package??

the split command returns
['<[email protected]> ','','','2338 * Re: PCB Pad Size for TQFP Package??']

instead of
['<[email protected]>, '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.
 
S

Serge Orlov

Sambo said:
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.
 
G

George Sakkis

Serge said:
Sambo said:
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
 
S

Sambo

George said:
Serge Orlov wrote:

Sambo said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top