I'm trying to replace every multiline import inside a Python source file.. So, the source goes like
我正在尝试替换Python源文件中的每个多行导入。所以,源代码就像
from XXX import (
AAA,
BBB,
)
from YYY import (
CCC,
DDD,
EEE,
...
)
...other instructions...
and I'd like to get something like
我想得到类似的东西
from XXX import AAA, BBB
from YYY import CCC, DDD, EEE, ...
...other instructions...
I tried to use sed but it looks like it doesn't support non-greedy matching of the closing parenthesis, so it "eats" the second import.. :(
Any hint? Is this impossible with sed? Should I try with another tool?
我试图使用sed,但看起来它不支持右括号的非贪婪匹配,所以它“吃”第二次导入.. :(任何提示?这是不可能的sed?我应该尝试使用其他工具?
3 个解决方案
#1
1
Ummm... what's wrong with Python?
嗯...... Python有什么问题?
lineIter= iter(aFile)
for aLine in lineIter:
if aLine.startswith("import"):
if aLine.endswith("("):
for aModule in lineIter:
if aModule.endwith(")"):
break
print "import", aModule.strip()
else:
print aLine.stri()
else:
print aLine.strip()
#2
2
This might work for you:
这可能对你有用:
sed '/^from/,/^)/{H;//{x;/)/{s/[\n()]//g;s/ */ /g;s/,$//;p;x}};d}' source
from XXX import AAA, BBB
from YYY import CCC, DDD, EEE, ...
...other instructions...
#3
1
For posterity, here is a somewhat polished version of S.Lott's script (I'd have posted it as a comment, but it's too long ^^; ).. This version preserves indentation and produces a result closer to my example.
对于后人来说,这是一个有点精致的S.Lott脚本版本(我已将它作为评论发布,但它太长了^^;)..这个版本保留缩进并产生更接近我的例子的结果。
lineIter=iter(aFile) for aLine in lineIter: s = aLine.strip() if s.startswith("from ") and s.endswith("("): complete = s[:-1] for aModule in lineIter: m = aModule.strip() if m.endswith(")"): break complete += m.strip() print complete else: print aLine,
#1
1
Ummm... what's wrong with Python?
嗯...... Python有什么问题?
lineIter= iter(aFile)
for aLine in lineIter:
if aLine.startswith("import"):
if aLine.endswith("("):
for aModule in lineIter:
if aModule.endwith(")"):
break
print "import", aModule.strip()
else:
print aLine.stri()
else:
print aLine.strip()
#2
2
This might work for you:
这可能对你有用:
sed '/^from/,/^)/{H;//{x;/)/{s/[\n()]//g;s/ */ /g;s/,$//;p;x}};d}' source
from XXX import AAA, BBB
from YYY import CCC, DDD, EEE, ...
...other instructions...
#3
1
For posterity, here is a somewhat polished version of S.Lott's script (I'd have posted it as a comment, but it's too long ^^; ).. This version preserves indentation and produces a result closer to my example.
对于后人来说,这是一个有点精致的S.Lott脚本版本(我已将它作为评论发布,但它太长了^^;)..这个版本保留缩进并产生更接近我的例子的结果。
lineIter=iter(aFile) for aLine in lineIter: s = aLine.strip() if s.startswith("from ") and s.endswith("("): complete = s[:-1] for aModule in lineIter: m = aModule.strip() if m.endswith(")"): break complete += m.strip() print complete else: print aLine,