Python 3按部分读取文件

时间:2014-01-08 12:57:36

标签: python

我正在尝试创建一个脚本来读取我的routes.php文件并获取数据。

例如,我有一个带有ff的routes.php。数据:

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

2 个答案:

答案 0 :(得分:1)

也许这样才能让你开始:

#!/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');

答案 1 :(得分:0)

这应该让你开始:

#!/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'
    }
]

因此,根据您的第二次编辑,您仍需要解析注释,以提取所需的信息。但这不应该太复杂适应这个脚本。