2011-03-03 58 views
1

我想搜索字符串的第一个实例的文本文件,并返回第一行中的一切,但我当前的程序找到并返回字符串的最后一行。如何在使用Python的文本文件中查找字符串的第一个实例?

任何想法我需要做什么?

谢谢!

这是我的代码如下所示:

#Open search file as read only 
with open(fullpath, 'r') as searchfile: 

    #Clear variable names 
    partname = partsize = None 

    #Search file for strings, trim lines and save as variables 
    for line in searchfile: 

     if "PART FILE NAME" in line: 
      x = line 
      partname = x[18:-1] 

     if "PART SIZE" in line: 
      y = line 
      partsize = y[18:-1] 
    #Open csv file, write variables, close csv file 
    storefile = open("C:/Documents and Settings/Desktop/blue pega3.csv", 'a') 
    storefile.write("%s,%s,%s\n" %(partname, partsize, fullpath)) 
    storefile.close() 
    #Close search file 
    searchfile.close()   ` 
+0

您正在使用'with'语句,因此您不应该调用'searchfile.close()'。另外,它在循环中被调用,这显然是错误的。 – 2011-03-03 14:57:37

+0

难道你只是使用正则表达式吗? – Jordan 2011-03-03 14:59:02

回答

3

最简单的方法是检查是否partnamepartsize已经有超过None以外的值:

partname = partsize = None 

for line in searchfile: 

    if partname and partsize: 
     break 

    if "PART FILE NAME" in line and partname is None: 
     x = line 
     partname = x[18:-1] 

    if "PART SIZE" in line and partsize is None: 
     y = line 
     partsize = y[18:-1] 
+0

我只是写这个! +1 ... – John 2011-03-03 14:57:03

+0

尽管发现的变量是不必要的。如果它不是'None',它会被找到。 – John 2011-03-03 14:59:19

+0

修正了这个问题。 – 2011-03-03 15:02:17

0

试试这个:

partname = partsize = None 

#Search file for strings, trim lines and save as variables 
for line in searchfile: 

    if "PART FILE NAME" in line and partname = None: 
     x = line 
     partname = x[18:-1] 

    if "PART SIZE" in line and partsize = None: 
     y = line 
     partsize = y[18:-1] 
5

您的代码返回最后的匹配,因为你循环整个文件,不断覆盖partnamepartsize。你可能只是ovwerite他们,如果他们还没有定义:

partname = partsize = None 
with open(fullpath, 'r') as searchfile: 
    for line in searchfile: 
     if partname is None and "PART FILE NAME" in line: 
      partname = line[18:-1] 
     if partsize is None and "PART SIZE" in line: 
      partsize = line[18:-1] 
     if partname is not None and partsize is not None: 
      break 

最后if停止遍历该文件,如果这两条线都已经发现了 - 我们没有必要继续再搜索。

0

如果有一天你想支持多于2个模式则:

import csv 

d = {} # name -> found part 
patterns = ["PART FILE NAME", "PART SIZE", "part new"] 
fieldnames = ["partname", "partsize", "partnew"] 
names = dict(zip(patterns, fieldnames)) 

# find patterns in the file 
with open(fullpath) as file: 
    for line in file: 
     if not patterns: 
      break # nothing left to find 

     for i in reversed(range(len(patterns))): # iterate in reverse 
               # to allow `del` 
      if patterns[i] in line: 
       d[names[patterns[i]]] = line[18:-1] # found 
       del patterns[i] # search for the *first* instance only 

# save found values 
with open(outputpath, 'wb') as storefile: 
    writer = csv.DictWriter(storefile, fieldnames+['fullpath']) 
    d['fullpath'] = fullpath 
    writer.writerow(d) 
0

迟到了,对不起,但我认为这是很酷:

pattern = re.compile(r'search string') 
try: 
    with open('search file') as inf: 
     # Read each line from inf, calling pattern.search(line). 
     # ifilter() will keep reading until it gets a match object 
     # instead of None. next() will either return the first 
     # such match object, or raise StopIteration. 
     match = next(itertools.ifilter(None, 
             (pattern.search(line) 
             for line in inf))) 
except IOError as err: 
    # ... 
except StopIteration: 
    # ... 

try/except模糊了一点,但关键是这个单一的next()表达式或者提供了re.MatchObject或者提高了StopIteration

当然,和任何MatchObject一样,整个原始行可以检索为match.string

相关问题