2014-12-29 48 views
0

我的配置文件:在兩個分隔符之間打印文本?

$ cat ../secure/test.property 
#<TITLE>Connection setting 
#MAIN DEV 
jdbc.main.url= 
jdbc.main.username= 
jdbc.main.password= 

#<TITLE>Mail settings 
mail.smtp.host=127.0.0.1 
mail.smtp.port=25 
mail.smtp.on=false 

email.subject.prefix=[DEV] 

#<TITLE>Batch size for package processing 
exposureImportService.batchSize=10 
exposureImportService.waitTimeInSecs=10 

ImportService.batchSize=400 
ImportService.waitTimeInSecs=10 

#<TITLE>Other settings 
usePrecalculatedAggregation=true 

###################### Datasource wrappers, which allow to log additional information 
bean.datasource.query_log_wrapper=mainDataSourceWrapper 
bean.gpc_datasource.query_log_wrapper=gpcDataSourceWrapper 

time.to.keep.domain=7*12 
time.to.keep.uncompress=1 

#oracle max batch size 
dao.batch.size.max=30 

和功能,這回線 「#<TITLE>Other settings」(例如),選擇 「配置部分」。

接下來,需要打印所選「節」和下一行之間的所有行,startwith#<TITLE>

如何實現?

P.S.

def select_section(property_file): 

    while True: 

     with open(os.path.join(CONF_DIR, property_file), 'r+') as file: 

      text = file.readlines() 
      list = [] 

      print() 

      for i in text: 
       if '<TITLE>' in i: 
        line = i.lstrip('#<TITLE>').rstrip('\n') 
        list.append(line) 
        print((list.index(line)), line) 

      res_section = int(raw_input('\nPlease, select section to edit: ')) 
      print('You selected: %s' % list[res_section]) 

      if answer('Is it OK? '): 
       return(list[res_section]) 
       break 

它就像工作:

... 
0 Connection setting 
1 Mail settings 
2 Batch size for package processing 
3 Other settings 

Please, select section to edit: 
... 

和預期產出,如果選擇Connection setting

... 
0 jdbc.main.url 
1 jdbc.main.username 
2 jdbc.main.password 

Please, select line to edit: 
... 

回答

1

如果我理解正確的問題,這裏是因爲它讀取文件組裝請第一個解決方案:

def get_section(section): 
    marker_line = '#<TITLE>{}'.format(section) 
    in_section = False 
    section_lines = [] 
    with open('test.property') as f: 
     while True: 
      line = f.readline() 
      if not line: 
       break 
      line = line.rstrip() 
      if line == marker_line: 
       in_section = True 
      elif in_section and line.startswith('#<TITLE>'): 
       break 

      if in_section: 
       if not line or line.startswith('#'): 
        continue 
       section_lines.append(line) 
    return '\n'.join(['{} {}'.format(i, line) 
          for i, line in enumerate(section_lines)]) 

print get_section('Connection setting') 

輸出:

0 jdbc.main.url= 
1 jdbc.main.username= 
2 jdbc.main.password= 

或許這將讓你開始。

+0

謝謝,@xnx,雖然 - 我用ConfigParcer模塊解決了它:-) – setevoy

1

這裏有一個快速的解決方案:

def get_section(section): 
    results = '' 
    with open('../secure/test.property') as f: 
     lines = [l.strip() for l in f.readlines()] 

    indices = [i for i in range(len(lines)) if lines[i].startswith('#<TITLE>')] 

    for i in xrange(len(indices)): 
     if lines[indices[i]] == '#<TITLE>' + section: 
      for j in xrange(indices[i], indices[i+1] if i < len(indices)-1 else len(lines) - 1): 
       results += lines[j] + '\n' 
      break 

    return results 

你可以用它如:

print get_section('Connection setting') 

不是很優雅,但它的作品!

相關問題