我從包含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]');
'comment'中有什麼? – thefourtheye
@thefourtheye更新了我的問題。 – majidarif
我只得到'''註冊一個新用戶到組跳轉平臺'] – thefourtheye