473,395 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1497
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
1
by: Sam Johnson | last post by:
Hi I've seen that the string classes in the .net framework have a cool (new?) function: splitting strings and returning the substrings in an array. Is there any equivalent to this in "old-style"...
0
by: pmarisole | last post by:
I am using the following code to split/join values in a multi-select field. It is combining all the values in All the records into one long string in each record in recordset. Example: I have a...
6
by: py_genetic | last post by:
Hi, I'm looking to generate x alphabetic strings in a list size x. This is exactly the same output that the unix command "split" generates as default file name output when splitting large...
12
blazedaces
by: blazedaces | last post by:
Hello again. I'm trying to take as an input an ArrayList<String> and utilize String's .spit(delimiter) method to turn that into a String. I'm getting some kind of error though (I'll post the code...
7
by: Johny | last post by:
I have a string of a variable length and I need to split the string in strings of 6 characters . But if the 6th character is not space then I must split the string at possition before the 6th...
5
by: shaiful | last post by:
Hi all I have a simple problem with string. I want to split string, such as: dim s as string dim k(3) as string s="aa1,bv1,cc1,dt1" i want to split the value in k, k(0)=aa1 k(1)=bv1...
7
by: spoken | last post by:
Hi, I'm trying to read a file with data seperated by "|" character. Ex: 3578.27|2001|Road Bikes|Metchosin|Canada 3399.99|2001|Mountain Bikes|Pantin|France 3399.99|2001|Mountain...
4
by: dmitrey | last post by:
hi all, howto split string with both comma and semicolon delimiters? i.e. (for example) get from string "a,b;c" I have tried s.split(',;') but it don't work Thx, D.
4
by: N9 | last post by:
Hi Anyone who can help about split string. string text = "History about a boy, who loves to play baseball with his friends." I like to find indexOf "play" and read the string 10 char left...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.