2014-01-08 153 views
2

我從包含10行的文本中找到一行。Python 3刪除空列表

desc = re.findall(r'@description (.*)', comment.strip()) 

會發生什麼事是它返回@description但它也有9名空列表。

print(desc) 

回報:

[] 
[] 
[] 
[] 
[] 
[] 
[] 
[] 
['the desc is here'] 
[] 

那麼,如何擺脫那些空[]的,使desc=['the desc is here']


更新

我試圖名單過濾器還是一樣的回報

註釋包含:

/** 
* @param string username required the username of the registering user 
* @param string password required 
* @param string first_name required 
* @param string last_name required 
* @param string email  required 
* @package authentication 
* @info user registration 
* @description register a new user into the groupjump platform 
*/ 

更新

註釋是一個完整的字符串,所以我用線

comments = route['comment'] 
comments = list(filter(None, comments.split('\n'))) 

實際代碼

#!/usr/bin/env python3 

import re 

routes = [] 
description = '' 
with open('troutes.php', 'r') as f: 
    current_comment = '' 
    in_comment = False 
    for line in f: 
     line = line.lstrip() 
     if line.startswith('/**'): 
      in_comment = True 

     if in_comment: 
      current_comment += line 

     if line.startswith('*/'): 
      in_comment = False 

     if line.startswith('Route::'): 
      matches = re.search(r"Route::([A-Z]+)\('(.*)', '(.*)'\);", line) 
      groups = matches.groups() 
      routes.append({ 
       'comment': current_comment, 
       'method': groups[0], 
       'path': groups[1], 
       'handler': groups[2], 
      }); 
      current_comment = '' # reset the comment 

for route in routes: 

    # get comments 
    comments = route['comment'] 
    comments = list(filter(None, comments.split('\n'))) 

    for comment in comments: 
    params = re.findall(r'@param (.*)', comment.strip()) 
    object = re.findall(r'@package (.*)', comment.strip()) 
    info = re.findall(r'@info (.*)', comment.strip()) 
    desc = re.search(r'@description (.*)', comment.strip()) 

    print(comment[15:]) 
讀取

數據拆分它像這樣這樣我就可以讀行:

<?php 
/** 
* @param string username required the username of the registering user 
* @param string password required 
* @param string first_name required 
* @param string last_name required 
* @param string email  required 
* @package authentication 
* @info user registration 
* @description register a new user into the groupjump platform 
*/ 
Route::POST('v3/register', '[email protected]'); 

/** 
* @param string username required the username of the registering user 
* @param string password required 
*/ 
Route::GET('v3/login', '[email protected]'); 
+0

'comment'中有什麼? – thefourtheye

+0

@thefourtheye更新了我的問題。 – majidarif

+1

我只得到'''註冊一個新用戶到組跳轉平臺'] – thefourtheye

回答

0

看起來像你' e逐行匹配圖案。你爲什麼不反對整個評論?

>>> comment = '''/** 
... * @param string username required the username of the registering user 
... * @param string password required 
... * @param string first_name required 
... * @param string last_name required 
... * @param string email  required 
... * @package authentication 
... * @info user registration 
... * @description register a new user into the groupjump platform 
... */''' 
>>> 
>>> import re 
>>> desc = re.findall(r'@description (.*)', comment) 
>>> desc 
['register a new user into the groupjump platform'] 
+0

它不是真的顯示。它只是使變量'desc = desc' – majidarif

+0

@majimboo,你可以顯示完整的代碼? – falsetru

+0

@majimboo,我更新了答案。 – falsetru

1

一種單一的列表條件就是:

if desc: 
    print(desc) 

它的簡寫版本:

if len(desc) > 0: 
    print(desc) 

對於列出的清單是:

desc = [d for d in desc if d] 

要獲得只有字符串,請執行以下操作:

if desc: 
    print(desc[0]) 
+0

不,另一個列表仍然存在 – majidarif

+0

@majimboo查看我的編輯。 – BartoszKP

0

您可以用列表的一個列表理解列表中的一個空字符串過濾器列表:

desc = re.findall(r'@description (.*)', comment.strip()) 
desc = [d for d in desc if len(d[0]) > 0] 

另一種解決方案是隻有在第一個元素包含一些打印的元素:

desc = re.findall(r'@description (.*)', comment.strip()) 
for d in desc: 
    if len(d) > 0 and d[0]: # check if there's a first element and if this element isn't empty 
      print d 
0

要讓您的代碼正常工作,您需要處理單個字符串,如果您有10行代碼需要這樣做:

joined = "\n".join(lines) 
for i in re.findall(r'@description (.*)', joined): 
    print (i)