JYTHON 2.0 代码-字符串操作和替换

发布于 2024-08-14 05:51:14 字数 899 浏览 4 评论 0原文

因为我已经问过这个问题了 我后来意识到这个工具 我正在为此编写 JYTHON 代码 目前支持到2.1版本 由于解释器是 2.1 所以一些 先进的技术不起作用。

现在是一名新人,很高兴学习 jython 中的更多内容,以便我可以写更多内容 更好、更智能的代码。

  1. jython 中的 FOR 循环比 while 循环更快:

  2. 我有一个从 XML 文件中获取的长字符串,看起来像 这个

CDATA[EMP_ID]]

我希望这是

CDATA[TRIM(EMP_ID)]

简而言之,只需在周围添加 TRIM() 不断变化的列名称,

做我用过的一侧 更换

替换('CDATA[TRIM(')

我正在寻找如何放置的答案 右括号,无论数字如何 开括号后的字符数。

我搜索时想到的技术 对于 IF SUB (LINE,1,52)=='CDATA[' 然后替换 ']]>'和 ')]]>>'解决我的需要。

当我尝试使用 STARTWITH 进行搜索时 ,SUB 也给我带来了其他字符串 因为它们与该行的某些部分匹配。

简而言之,我的问题

  1. 有简单的方法吗?

  2. 如果我是对的,如何才能使用正确的搜索技术。

再次感谢大家,这 论坛确实给了我很多帮助 学习 jython 并纠正和 向我展示正确的方法。

再次感谢您的帮助。

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.

  1. Is FOR LOOP faster in jython than while loop:

  2. 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

  1. Is there an easy way to do it ?

  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-08-21 05:51:14

在这种情况下,一个简单的正则表达式可能就足够了:

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()

列名使用 '[A-Z_]+' regex 进行匹配,即一个或多个大写字母或 '_'。请参阅 re 模块 的文档。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文