简体   繁体   English

使用python解析文本文件,并在唯一的单词之间抓取单词

[英]Parse through text files, and grab words in between unique words, using python

I am trying to build a parser that will go through some SQL scripts and grab the data fields, tables etc. being used. 我正在尝试构建一个解析器,该解析器将通过一些SQL脚本并获取正在使用的数据字段,表等。 I started it by trying to grab the field names. 我首先尝试获取字段名称。 The SQL scripts all have a basic structure of: SQL脚本均具有以下基本结构:

select x,y,z,.. from table # sometimes it will be sel instead of select

This occurs usually multiple times in any script. 在任何脚本中,这种情况通常发生多次。

I have setup the below python code: 我已经设置了以下python代码:

import csv
import re

def parser():                                     

           f=open('Book1.txt','r')
           data = f.read()
           print re.findall('sel.*from',data) 

I am only getting one of the select statements through this. 通过这种方式,我只能得到其中一条选择语句。 Why is this not giving me all the texts between my select statements from which I can then parse through and determine the data fields being used? 为什么这不能给我选择语句之间的所有文本,然后我可以从中进行解析并确定所使用的数据字段? Maybe there is a better way to do this but I am hitting a wall. 也许有更好的方法可以做到这一点,但是我碰壁了。

Your regular expression is probably matching multiple select statements because it's working in greedy mode. 您的正则表达式可能与多个select语句匹配,因为它在贪婪模式下工作。 Try using re.findall('sel.*?from', data) instead. 尝试改用re.findall('sel.*?from', data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python的2个文件之间最常见的单词 - most common words between 2 files using Python 如何使用python在文本文件包中为每个文本文件找到唯一的单词? - How to find unique words for each text file in a bundle of text files using python? 如何使用 Python 从文本文件中返回唯一的单词 - How to return unique words from the text file using Python 如何搜索文本文件的文件夹以查看是否存在特定字符串,然后使用Python提取两个单词之间的字符串? - How to search a folder of text files to see if a specific string exists and then extract a string between two words using Python? 使用python多次打印文本文件中两个单词之间的文本 - Print Text between two words in a text file multitime using python 计算文本文件中的唯一单词 (Python) - Count unique words in a text file (Python) 从Python中的文本文件中获取某些单词和短语 - Grab certain words and phrases from a text file in Python 使用 Python 提取给定单词集之间的文本 - Extract text between given set of words using Python 使用Python从文本文件中创建n个单词的(随机)样本 - Using Python to create a (random) sample of n words from text files 使用 python 替换文本中的单词 - Replace words in a text using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM