2011-03-03 49 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

相關問題