Python3通过p读取文件

2024-04-18 18:02:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个脚本来阅读我的路由.php归档并获取数据。你知道吗

例如,我有一个路由.php用ff。数据:

/**
 * @param string username   required 
 * @param string password   required
 * @param string first_name required
 * @param string last_name  required
 * @param string email      required
 */
Route::POST('/register', 'UserController@Register');

/**
 * @param string username   required 
 * @param string password   required
 * @param string first_name required
 * @param string last_name  required
 * @param string email      required
 */
Route::POST('/login', 'UserController@login');

让我们假设它们都是不同的路线。你知道吗

现在我想得到从/**);的每条路由

在这个示例中,它返回2个路由,我只需要使用已有的函数获取路由url、方法和参数。你知道吗

唯一的问题是如何读取每个路由的文件?你知道吗

# read troutes.php
routes = open('troutes.php', 'r')

# do stuff to each routes
print(routes.read())

# close troutes.php
routes.close()

更新

我试图逐行读取文件,但问题是如何获取路由上方的注释并将其与路由关联。你知道吗

for line in open('routes.php'):
    li = line.strip()
    if li.startswith('Route::'):

    # print method
    method = find_between( li, "Route::", "(" )

更新

python应该返回如下2个路由:

Method: POST
Url: /register
Parameter:
     username
        type: string
        required: True
     password
        type: string
        required: True

Method: POST
Url: /login
......so on per route

Tags: name路由stringparamrequiredusernameloginpassword
2条回答

这应该让你开始:

#!/usr/bin/env python3

import re

routes = []
with open('routes.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

print(routes)

输出为:

[
    {
        'comment': '/**\n* @param string username   required \n* @param string password   required\n* @param string first_name required\n* @param string last_name  required\n* @param string email      required\n*/\n',
        'path': '/register',
        'handler': 'UserController@Register',
        'method': 'POST'
    },
    {
        'comment': '/**\n* @param string username   required \n* @param string password   required\n* @param string first_name required\n* @param string last_name  required\n* @param string email      required\n*/\n',
        'path': '/login',
        'handler': 'UserController@login',
        'method': 'POST'
    }
]

因此,基于您的第二次编辑,您仍然需要解析注释,以提取所需的信息。但改编这个剧本应该不会太复杂。你知道吗

也许这样可以让你开始:

#!/usr/bin/env python

with open("input") as fd:
    data = [i.strip() for i in fd]

D = []
tmp = []
for i in data:
    tmp.append(i)
    if ';' in i:
        D.append(tmp)
        tmp = []

print D[0][-1]
print D[1][-1]

输出:

Route::POST('/register', 'UserController@Register');
Route::POST('/login', 'UserController@login');

相关问题 更多 >