How to extract certain strings start with a symbol from a text file in python? -
how can extract words start symbol '$' text file?
file (ascii) -->
@extendedattr = nvp_add(@extendedattr, "severity", $severity, "description", $description, "eventid", $eventid, "eventurl", $eventurl, "alertlevel", $alertlevel, "eventstart", $eventstart, "eventsourcecount", $eventsourcecount, "eventsourcetable", $eventsourcetable, "eventdestcount", $eventdestcount)
i want output (all in new line) :
$severity $description $eventid $eventurl $alertlevel $eventstart $eventsourcecount $eventsourcetable $eventdestcount
use regex
:
>>> import re >>> open('filename') f: ... ans = [] ... line in f: ... matches = re.findall(r'(?<!\w)(\$\w+)', line) ... ans.extend(matches) ... >>> print ans ['$severity', '$description', '$eventid', '$eventurl', '$alertlevel', '$eventstart', '$eventsourcecount', '$eventsourcetable', '$eventdestcount']
now use str.join
expected output:
>>> print "\n".join(ans) $severity $description $eventid $eventurl $alertlevel $eventstart $eventsourcecount $eventsourcetable $eventdestcount
Comments
Post a Comment