简体   繁体   中英

Python 3 remove empty list

I am finding a line from a text that has 10 lines.

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

What happens is it returns the @description but it also has 9 empty lists.

print(desc)

returns:

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

So how do I get rid of those empty [] and make desc=['the desc is here'] ?


update

I tried list filter and still the same return

The comment contains:

/**
 * @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
 */

update

comment is a full string, so I split it like this so I can read line by line

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

actual code

#!/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:])

data being read:

<?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', 'UserController@Register');

/**
 * @param string username   required    the username of the registering user 
 * @param string password   required
 */
Route::GET('v3/login', 'UserController@login');

A condition for a single list is just:

if desc:
    print(desc)

It is a shorthand version of:

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

For a list of lists it's:

desc = [d for d in desc if d]

To get only the string do this:

if desc:
    print(desc[0])

It seems like you're matching the pattern line by line. Why don't you match against the whole comment?

>>> 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']

You can filter list with an empty string from a list of lists with a list comprehension:

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

Another solution is to print the element only if the first element contains something:

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

To get your code to work you need to work on a single string, if you have 10 lines do like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM