Я нахожу линию из текста с 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]');
Что есть в 'комментарии'? – thefourtheye
@ thefourtheye обновил мой вопрос. – majidarif
Я получаю только, '['зарегистрировать нового пользователя на платформе groupjump']' – thefourtheye