首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动格式化python代码:字典

自动格式化python代码:字典
EN

Stack Overflow用户
提问于 2014-08-22 03:49:02
回答 2查看 99关注 0票数 2

坦率地说,我有一个极具魔力的python源文件,其中包含无穷无尽的无缩进字典。

它是不可读的。我想重新格式化这个文件,使其看起来像这样:

代码语言:javascript
复制
[
    {
        'foo'     : 'bar',
        'alpha'   : 'beta',
        'long key': 'bla'
    }

    # 93457823 more dictionaries
]

注意对齐的:,以及人类准备好的缩进。

有没有可以做到这一点的工具?

EN

回答 2

Stack Overflow用户

发布于 2014-08-22 03:53:15

Python语言中定义了一个PEP8标准,规定了如何格式化代码。还有一个工具可以执行这个标准"pep8":

https://pypi.python.org/pypi/autopep8/

文档中有以下杂乱的代码:

代码语言:javascript
复制
import math, sys;

def example1():
    ####This is a long comment. This should be wrapped to fit within 72 characters.
    some_tuple=(   1,2, 3,'a'  );
    some_variable={'long':'Long code lines should be wrapped within 79 characters.',
    'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
    'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
    20,300,40000,500000000,60000000000000000]}}
    return (some_tuple, some_variable)
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
class Example3(   object ):
    def __init__    ( self, bar ):
     #Comments should have a space after the hash.
     if bar : bar+=1;  bar=bar* bar   ; return bar
     else:
                    some_string = """
                       Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
                    return (sys.path, some_string)    import math, sys;

def example1():
    ####This is a long comment. This should be wrapped to fit within 72 characters.
    some_tuple=(   1,2, 3,'a'  );
    some_variable={'long':'Long code lines should be wrapped within 79 characters.',
    'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
    'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
    20,300,40000,500000000,60000000000000000]}}
    return (some_tuple, some_variable)
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
class Example3(   object ):
    def __init__    ( self, bar ):
     #Comments should have a space after the hash.
     if bar : bar+=1;  bar=bar* bar   ; return bar
     else:
                    some_string = """
                       Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
                    return (sys.path, some_string)

转换为:

代码语言:javascript
复制
import math
import sys


def example1():
    # This is a long comment. This should be wrapped to fit within 72
    # characters.
    some_tuple = (1, 2, 3, 'a')
    some_variable = {
        'long': 'Long code lines should be wrapped within 79 characters.',
        'other': [
            math.pi,
            100,
            200,
            300,
            9876543210,
            'This is a long string that goes on'],
        'more': {
            'inner': 'This whole logical line should be wrapped.',
            some_tuple: [
                1,
                20,
                300,
                40000,
                500000000,
                60000000000000000]}}
    return (some_tuple, some_variable)


def example2():
    return ('' in {'f': 2}) in {'has_key() is deprecated': True}


class Example3(object):

    def __init__(self, bar):
        # Comments should have a space after the hash.
        if bar:
            bar += 1
            bar = bar * bar
            return bar
        else:
            some_string = """
                       Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
            return (sys.path, some_string)
票数 1
EN

Stack Overflow用户

发布于 2014-08-22 03:56:26

您可以尝试将.py脚本转换为pep8格式,如下所述:Tool to convert Python code to be PEP8 compliant

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25434754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档