Re: strip not working on strings?
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] wrote: > I'm using Python 2.3.5 and when I type the following in the interactive > prompt I see that strip() is not working as advertised: > > >>>s = 'p p:p' > >>>s.strip(' :') > 'p p:p' > > Is this just me or does it not work? I want to get rid of all ' ' and > ':' in the string. I've checked the doc and from what I can tell this > is what strip() is supposed to do. In /my/ docs, s.strip return a copy of s where all /leading/ and /heading/ spaces are removed. s.strip(x) does the same but removing chars of x. So, what you're asking for by s.strip(' :') is "remove heading or leading space or ':' chars", /not/ "remove heading or leading space or ':' chars". If you want to get rid of ' ' and ':' anywhere in s, i think that string.maketrans and s.translate will do the job: >>> import string >>> s = 'p p:p' >>> ident = string.maketrans('', '') >>> s.translate(ident,' :') 'ppp' -- Jaco -- http://mail.python.org/mailman/listinfo/python-list
Re: strip not working on strings?
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] wrote: > I'm using Python 2.3.5 and when I type the following in the interactive > prompt I see that strip() is not working as advertised: > > >>>s = 'p p:p' > >>>s.strip(' :') > 'p p:p' > > Is this just me or does it not work? I want to get rid of all ' ' and > ':' in the string. I've checked the doc and from what I can tell this > is what strip() is supposed to do. In /my/ docs, s.strip return a copy of s where all /leading/ and /heading/ spaces are removed. s.strip(x) does the same but removing chars of x. So, what you're asking for by s.strip(' :') is "remove heading or leading space or ':' chars", /not/ "remove or leading or ':' chars". If you want to get rid of ' ' and ':' anywhere in s, i think that string.maketrans and s.translate will do the job: >>> import string >>> s = 'p p:p' >>> ident = string.maketrans('', '') >>> s.translate(ident,' :') 'ppp' -- Jaco -- http://mail.python.org/mailman/listinfo/python-list
Re: strip not working on strings?
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] wrote: > I'm using Python 2.3.5 and when I type the following in the interactive > prompt I see that strip() is not working as advertised: > > >>>s = 'p p:p' > >>>s.strip(' :') > 'p p:p' > > Is this just me or does it not work? I want to get rid of all ' ' and > ':' in the string. I've checked the doc and from what I can tell this > is what strip() is supposed to do. In /my/ docs, s.strip return a copy of s where all /leading/ and /heading/ spaces are removed. s.strip(x) does the same but removing chars of x. So, what you're asking for by s.strip(' :') is "remove heading or leading space or ':' chars", /not/ "remove space or ':' chars". If you want to get rid of ' ' and ':' anywhere in s, i think that string.maketrans and s.translate will do the job: >>> import string >>> s = 'p p:p' >>> ident = string.maketrans('', '') >>> s.translate(ident,' :') 'ppp' -- Jaco -- http://mail.python.org/mailman/listinfo/python-list
Re: about sort and dictionary
Shi Mu <[EMAIL PROTECTED]> writes: > #why c can not append the sorted b?? Because sort() doesn't return anything? According to the library reference: 7) The sort() and reverse() methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don't return the sorted or reversed list. -- Eric Jacoboni, ne il y a 1435934131 secondes -- http://mail.python.org/mailman/listinfo/python-list
Re: Underscores in Python numbers
Mike Meyer <[EMAIL PROTECTED]> writes: > I've seen at least one language (forget which one) that allowed such > separators, but only for groups of three. So 123_456 would be valid, > but 9_1 would be a syntax error. Ada allows underscores in numeric literals since 1983, without enforcing any grouping. The Ruby language allows also this notation. You may write 1_000_001 or 1000_001 or 10_00_001, etc. (the same for real numbers...). When you have the habit to represent literals like that, all other big numeric literals or workarounds to create grouping seem cryptic. -- Eric Jacoboni, ne il y a 1435938104 secondes -- http://mail.python.org/mailman/listinfo/python-list
Re: Underscores in Python numbers
[EMAIL PROTECTED] (Bengt Richter) writes: >>Eric Jacoboni, ne il y a 1435938104 secondes > Um, about your sig ... ;-) Well, i confess it's Ruby code... Maybe, one day, i will try to write a Python Version (with DateTime, i guess?) but i'm afraid it doesn't change the result. -- Eric Jacoboni, ne il y a 1436041406 secondes -- http://mail.python.org/mailman/listinfo/python-list
Question about struct.unpack
Hi, To experiment with unpacking, i've written a little C code which stores one record in a file. Then, i try to reread this file to unpack the record. Here's the struct of a record: typedef struct { char nom[30]; double taille; int age; char plop; } enreg_t; The whole size, as given by sizeof() is 48, due to byte alignment. I was first thinking that "32sdic" would make the job, but calcsize() reports only 45 for this format. So, without knowing what, i've tried "32sdicxxx" to reach the 48 expected... Now it works... The same file, re-read with a Ruby script needs a str.unpack("Z32dIc"). So, i don't know why i need to pad the format string in Python. Any clue? BTW: how to get rid of all this stuff after the \0 in the first field in Python? (Ruby has Z and A, but it seems that the Python 's' specifier is like 'A' and there is no 'Z' equivalent) -- Eric Jacoboni, ne il y a 1444057108 secondes -- http://mail.python.org/mailman/listinfo/python-list
Re: Question about struct.unpack
Scott David Daniels <[EMAIL PROTECTED]> writes: Thanks for your explanations. But : > nom = nomz.rstrip('\0') doesn't work for me: >>> nomz 'Dupont\x00\x80\xbf\xff\xf70\x8f\xe0u\xa4\x00\x00.8\xfe\xfe\xfe\xff\x80\x80\x80\x80' >>> nom = nomz.rstrip('\0') >>> nom 'Dupont\x00\x80\xbf\xff\xf70\x8f\xe0u\xa4\x00\x00.8\xfe\xfe\xfe\xff\x80\x80\x80\x80' -- Eric Jacoboni, ne il y a 1444080064 secondes -- http://mail.python.org/mailman/listinfo/python-list