Python 更换Jython中的多条线路
我已经编写了一个小程序来替换一组字符,但是我还希望在一个程序中使用两个或更多的replace命令 除此之外,我还想在随机字符集后添加一个括号 这是我的节目Python 更换Jython中的多条线路,python,string,replace,jython,Python,String,Replace,Jython,我已经编写了一个小程序来替换一组字符,但是我还希望在一个程序中使用两个或更多的replace命令 除此之外,我还想在随机字符集后添加一个括号 这是我的节目 file_read=open('<%=odiRef.getOption("READ")%>/EXPORT.XML','r') file_write=open('<%=odiRef.getOption("READ")%>/EXPORT_1.XML','w') count_record=file_read.read() w
file_read=open('<%=odiRef.getOption("READ")%>/EXPORT.XML','r')
file_write=open('<%=odiRef.getOption("READ")%>/EXPORT_1.XML','w')
count_record=file_read.read()
while count_record :
s=count_record.replace('<Field name="ExeDb"type="java.lang.String"><![CDATA[S]]></Field>','<Field name="ExeDb" type="java.lang.String"><![CDATA[W]]></Field>')
file_write.write(s)
t=count_record.replace('<Field name="Txt" type="java.lang.String"><![CDATA[','<Field name="Txt" type="java.lang.String"><![CDATA[TRIM(')
file_write.write(t)
count_record=file_read.read()
print s
file_read.close()
file_write.close()
file_read=open('/EXPORT.XML',r')
文件_write=open('/EXPORT_1.XML',w')
count\u record=file\u read.read()
计数记录时:
s=计数\记录。替换(“”,“”)
文件_write.write(s)
t=count\u record.replace('这在许多级别上都是错误的-不能同时从同一个文件读取和写入,file.read()命令读取整个内容,并且在每次替换后不必保存。类似于以下内容:
file = open('myfile', 'r+')
contents = file.read()
file.seek(0) # rewind
file.write(contents.replace('something', 'else').replace('and this too', 'replaced'))
注释代码是乱码,包括这里…您需要使用正则表达式替换它。请参阅模块“re”说明,您基本上需要如下内容:
import re
contents = re.sub(
'<Field name="Txt" type="java.lang.String"><!\[CDATA\[TRIM\(([^)]*)\]\]></Field>',
'<Field name="Txt" type="java.lang.String"><![CDATA[TRIM(\1)]]></Field>',
contents
)
重新导入
contents=re.sub(
'',
'',
目录
)
这在许多级别上都是错误的-您不能同时从同一个文件读取和写入,file.read()命令读取整个内容,并且您不必在每次替换后进行保存。类似于以下内容:
file = open('myfile', 'r+')
contents = file.read()
file.seek(0) # rewind
file.write(contents.replace('something', 'else').replace('and this too', 'replaced'))
注释代码是乱码,包括这里…您需要使用正则表达式替换它。请参阅模块“re”说明,您基本上需要如下内容:
import re
contents = re.sub(
'<Field name="Txt" type="java.lang.String"><!\[CDATA\[TRIM\(([^)]*)\]\]></Field>',
'<Field name="Txt" type="java.lang.String"><![CDATA[TRIM(\1)]]></Field>',
contents
)
重新导入
contents=re.sub(
'',
'',
目录
)
非常感谢kibitzer,我还有一个问题,我已经提到,我需要找到一个字符串,即]]>在上面的搜索字符串中,***表示任意数量的长度不同的字符,我希望在这个未知字符后加一个紧括号。因此,最后我的字符串应该是)]]>由于字符长度可变,我无法使用子字符串。非常感谢kibitzer,我还有一个问题,我已经提到,我需要找到一个字符串,即]]>在上述搜索字符串中,***表示任意数量的长度可变的字符,我想在这个未知字符后面加一个右括号。因此,最后我的字符串应该是)]]>由于字符长度不同,我无法使用子字符串。