我需要逐行读取一个文件,我需要查看"下一行",所以首先我将文件读入列表,然后循环浏览列表...不知怎的,这似乎很粗鲁,建立列表可能是变得昂贵.
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)
必须有更好的方法来迭代线,但我不知道如何向前看
刚刚写了这个函数......
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'够',建议?
我正在使用烧瓶和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)