JYTHON 2.0 CODE -STRING MANIPULATION and replace
As i have already asked this question and i have later realized that tool for which iam writing JYTHON CODES supports presently on till 2.1 version as the intepreter is of 2.1 so some of the advanced technique is not working.
Now being a new and excited to learn more in jython so that ican write more better and smarted code.
Is FOR LOOP faster in jython than while loop:
I have a long string taken from an XML file which looks something like this
CDATA[EMP_ID]]
and i want this to be
CDATA[TRIM(EMP_ID)]
in short just add TRIM() around the COLUMN NAME which keep changing ,
to do the one side I HAVE USED REPLACE
REPLACE ( 'CDATA[TRIM(')
i searching answer for how to put the close bracket irrespective any number of characters after open bracket.
The technique i thought if i search 开发者_运维百科 for IF SUB (LINE,1,52 )=='CDATA[ ' AND THEN REPLACE ']]>' WITH ')]]>>' solving my need.
As iam trying to search with STARTWITH ,SUB its getting me other strings too as they match some part of the line.
In short my question
Is there an easy way to do it ?
If iam right , how can use the right search technique .
Thanks again to all of you , this forums have really helped me a lot in learning jython and correcting and showing me the right way.
Thanks again for all your help.
A simple regular expression might be sufficient in this case:
import re
f = open('your_file.xml')
try:
xml = f.read()
xml_with_trim = re.sub(r'(CDATA\[)([A-Z_]+)(\]\])', r'\1TRIM(\2)\3', xml)
print xml_with_trim
finally:
f.close()
The column name is matched using '[A-Z_]+' regex
i.e., one or more capital letters or '_'
. See documentation for the re module.
精彩评论