I'm trying to replace every multiline import inside a Python source file.. So, the source goes like
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?
Ummm... what's wrong with 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()