user2671444
user2671444

Reputation: 15

How to extract certain strings start with a symbol from a text file in python?

How can i extract all words start from symbol '$' from a text file?

File a (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 the output to be like this (all in new line) :

$severity
$description
$eventID
$eventURL
$alertLevel
$eventStart
$eventSourceCount
$eventSourceTable
$eventDestCount

Upvotes: 1

Views: 1368

Answers (2)

sheba
sheba

Reputation: 850

Use regular expressions, noticing the escaping of $ (usually line-end) with \. Read the entire file at once with f.read() (Which can also be extracted to another line for enhanced readability)

import re

with open("filename", "r") as f:
...     matches = re.findall("(\$\w+)", f.read())
print matches

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

Use regex:

>>> import re
>>> with open('filename') as f:
...     ans = []
...     for 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 to get the expected output:

>>> print "\n".join(ans)
$severity
$description
$eventID
$eventURL
$alertLevel
$eventStart
$eventSourceCount
$eventSourceTable
$eventDestCount

Upvotes: 2

Related Questions