我从一个有10行的文本中找到一行。
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', 'UserController@Register');
/**
* @param string username required the username of the registering user
* @param string password required
*/
Route::GET('v3/login', 'UserController@login');
答案 0 :(得分:1)
单个列表的条件是:
if desc:
print(desc)
这是一个速记版本:
if len(desc) > 0:
print(desc)
列表是:
desc = [d for d in desc if d]
要仅获取字符串,请执行以下操作:
if desc:
print(desc[0])
答案 1 :(得分:0)
似乎你逐行匹配模式。你为什么不匹配整个评论?
>>> 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']
答案 2 :(得分: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
答案 3 :(得分:0)
要让你的代码工作,你需要处理一个字符串,如果你有10行像这样:
joined = "\n".join(lines)
for i in re.findall(r'@description (.*)', joined):
print (i)