1

如何从文本文件中提取从符号“$”开始的所有单词?

文件 a (ascii) -->

        @ExtendedAttr = nvp_add(@ExtendedAttr, "severity", $severity,  
 "description", $description, "eventID", $eventID,
             "eventURL", $eventURL, "alertLevel", $alertLevel, 
      "eventStart", $eventStart,
             "eventSourceCount", $eventSourceCount, "eventSourceTable", 
$eventSourceTable, "eventDestCount", $eventDestCount)

我希望输出是这样的(全部在新行中):

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

2 回答 2

2

使用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']

现在使用str.join来获得预期的输出:

>>> print "\n".join(ans)
$severity
$description
$eventID
$eventURL
$alertLevel
$eventStart
$eventSourceCount
$eventSourceTable
$eventDestCount
于 2013-08-16T07:28:18.557 回答
0

使用正则表达式,注意$(通常是行尾)用\. 一次读取整个文件f.read()(也可以将其提取到另一行以增强可读性)

import re

with open("filename", "r") as f:
...     matches = re.findall("(\$\w+)", f.read())
print matches
于 2013-08-16T08:13:37.033 回答