Python 使用scanString解析扫描文件

Python 使用scanString解析扫描文件,python,pyparsing,Python,Pyparsing,我正试图用pyparsing扫描一个文件。pyparsing中有一个名为scanString的方法,它确实有 >>> name = pp.Word(pp.alphas) >>> text = "**** Farcical aquatic ceremony" >>> for result, start, end in name.scanString(text): ... print "Found {0} at

我正试图用pyparsing扫描一个文件。pyparsing中有一个名为scanString的方法,它确实有

   >>> name = pp.Word(pp.alphas)
   >>> text = "**** Farcical aquatic  ceremony"
   >>> for result, start, end in name.scanString(text):
   ...     print "Found {0} at [{1}:{2}]".format(result, start, end)
   ... 
   Found ['Farcical'] at [5:13]
   Found ['aquatic'] at [14:21]
   Found ['ceremony'] at [23:31]

但是我想扫描一个包含句子中单词的txt文件。上面的扫描字符串扫描单词所在句子中的单词。有没有扫描文件的方法?或者,scanString是否仅像上面那样工作

你能再详细一点吗?你想问什么还不清楚;我有一个example.txt文件,我有一些像“我喜欢东京”这样的句子,我想在example.txt文件中搜索这些单词(“我”、“喜欢”、“东京”)。我可以用scanString方法来做这件事吗@hashcode55
scanString
仅适用于字符串,但将文本文件的内容读入字符串,然后从字符串中取出并不困难(假设文件大小不是千兆字节)。除了
scanString
之外,还可以使用其他方法,例如
searchString
parseString
,和
parseFile
。查看在线文档,你能详细说明一下吗?不清楚你想问什么;我有一个example.txt文件,我有一些像“我喜欢东京”这样的句子,我想在example.txt文件中搜索这些单词(“我”、“喜欢”、“东京”)。我可以用scanString方法来做这件事吗@hashcode55
scanString
仅适用于字符串,但将文本文件的内容读入字符串,然后从字符串中取出并不困难(假设文件大小不是千兆字节)。除了
scanString
之外,还可以使用其他方法,例如
searchString
parseString
,和
parseFile
。请参阅以下网址的在线文档: