小编Pau*_*aul的帖子

文件访问前进

我需要逐行读取一个文件,我需要查看"下一行",所以首先我将文件读入列表,然后循环浏览列表...不知怎的,这似乎很粗鲁,建立列表可能是变得昂贵.

for line in open(filename, 'r'):
    lines.append(line[:-1])

for cn in range(0, len(lines)):
    line = lines[cn]
    nextline = lines[cn+1] # actual code checks for this eof overflow
Run Code Online (Sandbox Code Playgroud)

必须有更好的方法来迭代线,但我不知道如何向前看

python

5
推荐指数
1
解决办法
2775
查看次数

寻找更多的pythonic apporach

刚刚写了这个函数......

def nrofleadingchars(stringtotest, testchar='\t'):
    count = 0
    for c in stringtotest:
        if c == testchar:
            count = count + 1
        else:
            return count
    return count
Run Code Online (Sandbox Code Playgroud)

然而,感觉不到pythonic'够',建议?

python

3
推荐指数
2
解决办法
101
查看次数

如何防止werkzeug登录

我正在使用烧瓶和werkzeug.为了监视从sqlalchemy发出的sql语句,我设置了一个logging.basicConfig()记录器并附加了before_cursor_execute事件来监视SQL语句.但现在werkzeug还将日志记录附加到该记录器,这不是我想要的.所以我的日志看起来像这样......(不想要werkzeug消息)

INFO:root:SELECT anon_1.heartbeat_id AS anon_1_heartbeat_id
FROM (SELECT heartbeat.id AS heartbeat_id FROM heartbeat ORDER BY stamp desc LIMIT ?
OFFSET ?) AS anon_1 ORDER BY heartbeat_name
INFO:werkzeug:127.0.0.1 - - [13/Jun/2013 12:10:52] "GET / HTTP/1.1" 200 -
Run Code Online (Sandbox Code Playgroud)

在werkzeug文档中,我找不到任何有关日志记录的信息.这是我正在使用的代码.

logging.basicConfig(filename='sql.log', level=logging.INFO)
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
    logging.info(statement)
event.listen(engine, "before_cursor_execute", before_cursor_execute)
Run Code Online (Sandbox Code Playgroud)

python logging werkzeug flask

3
推荐指数
2
解决办法
2641
查看次数

标签 统计

python ×3

flask ×1

logging ×1

werkzeug ×1