Converting = to ==

Jp Calderone exarkun at flashmail.com
Thu Apr 27 12:41:37 EDT 2000


Warren Postma wrote:
> 
> I have an "end user query tool" that uses Python expressions, but I want
> comparisons to be able to be written either as A=B or A==B then I want to
> compare them using Python's Eval function.
> 
> Problem is if you search and replace all = with == then users have to use
> the single-equals convention, and some of them will be python programmers,
> and will want to use the "non idiot mode" of the query tool.
> 
> So i did this, but is there a better way?
> 
>     >>> string.replace( string.replace("a=b a==b", "=", "=="), "====","==")
>     a==b a==b
> 
> Warren

 You can use regular expressions.  I'm not going to try to give you the
exact one off hand, because I'll get it wrong, but basically replace
"[anything-but-equals]=[anything-but-equals]" with "[first part]==[second
part]"
-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS dpu>-- s+: a--- C++++ UL++++ P+ L+++ E---- W++ N++ o+ K- w--- 
O-- M- V- PS+++ PE- Y+ PGP t+ 5+ X++ R+ tv+ b++ DI++ D--- 
G e* h! !r y-- 
------END GEEK CODE BLOCK------
--
 4:40pm up 29 days, 21:47, 0 users, load average: 0.11, 0.07, 0.01



More information about the Python-list mailing list