简体   繁体   English

在块内使用for循环时如何格式化Python

[英]How to format Python when using for loop inside with block

I've only recently starting using with to open files, instead of the more old-school separate open/close calls. 我只是最近才开始使用with来打开文件,而不是更老套的单独的open / close调用。

However, I'm finding this means all my code to iterate through files now has double-indentation: 但是,我发现这意味着我遍历文件的所有代码现在都具有双缩进:

with open('filename', 'rb') as f:
    for line in f:
        # do stuff

which is kinda ugly, compared to: 相比之下,这有点丑陋:

f = open('filename', 'rb')
for line in f:
    # do stuff
f.close()

I've tried: 我试过了:

with open('filename', 'rb') as f:
  for line in f:
    # do stuff

so that the "stuff" is only single indented, but this makes various lint checkers complain. 因此,“填充”仅是单缩进,但这使各种棉绒检查器抱怨。 Is there a better way of handling this? 有没有更好的方法来解决这个问题?

Note: I appreciate the extra benefits of context managers, I'm just after the best way to format the code. 注意:我感谢上下文管理器的额外好处,我只是追求格式化代码的最佳方法。

You don't have to put the logic inside the with block: 您不必将逻辑放在with块中:

with open('filename', 'rb') as f:
    file_content = f.readlines()

for line in file_content:
    # do stuff

The downside with this approach is that the whole file would need to be saved into the file_content list. 这种方法的缺点是整个文件需要保存到file_content列表中。

You can still have the benefit of the generator if you hide the read logic in a separate function: 如果您将读取逻辑隐藏在单独的函数中,则仍然可以受益于生成器:

def read_file(file_path):
    with open(file_path, 'rb') as f:
        for line in f:
            yield line
        # or simply `yield from f` in Python 3

for line in read_file(file_path):
    # do stuff

BUT all this is probably a total overkill for something as trivial as a two level indention. 但是,对于像两个级别的缩进这样微不足道的东西来说,所有这一切可能完全是矫kill过正。 You will be better off learning to live with it. 学习与之相处会更好。

To me, your original code is perfectly fine: 对我来说,您的原始代码非常好:

with open('filename', 'rb') as f:
    for line in f:
        # do stuff

However, if #do stuff becomes too big, I encourage you to use a function: 但是,如果#do stuff变得太大,建议您使用一个函数:

def do_stuff(f):
    for line in f:
        # do stuff

with open('filename', 'rb') as f:
    do_stuff(f)

When performance isn't an issue, it's very common to store the content of the file in a variable: 当性能不成问题时,通常将文件内容存储在变量中:

with open('filename', 'rb') as f:
    lines = f.readlines()

for line in lines:
    # do stuff

This is more useful when you need to manipulate several files at the same time and that you do not wish to nest several with structures. 当你需要同时操作多个文件,你不希望嵌套多个这是比较有用的with结构。

@Maxime's comment just beats it all: @Maxime的评论胜过一切:

def read_file(file_path):
    with open(file_path, 'rb') as f: 
        yield from f

for line in read_file('filename'):
    # do sth

This uses Python's yield from , explained at In practice, what are the main uses for the new "yield from" syntax in Python 3.3? 在实践中,这使用了Python的yield from在实践中解释了Python 3.3中新的“ yield from”语法的主要用途是什么? .

And to validate your concerns, here is the Zen of Python ( import this ) 为了验证您的关注,这里是Python的Zen( import this

 The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! 

It really gets ugly at four (?) and more levels of indentation. 在四个(?)及更高的缩进级别上,它确实变得难看。 In that case, you might be well-advised to clean this up, and move the whole file-handling code to a function, like so: 在这种情况下,建议您清理一下,然后将整个文件处理代码移到一个函数中,如下所示:

with open('filename', 'rb') as f:
    parse_file(f)

def parse_file(f):
    for line in f:
        # do stuff

It gets really deep when you start adding classes etc to the mix .... 当您开始向类中添加类等时,它真的变得很深。

Apart from that: do you have a nice editor, that eases the indentation etc? 除此之外:您是否有一个不错的编辑器,可以简化缩进等? While not for everyone, emacs is actually quite good with Python 虽然不是每个人都适合,但emacs实际上对Python相当不错

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM