Skip to main content
 首页 » 编程设计

python之自动格式化 python 代码 : dictionaries

2025年05月04日14wayfarer

我有一个非常恶魔般的 python 源文件,其中包含无穷无尽的未缩进字典列表。

这是不可读的。我想将文件重新格式化为如下所示:

[ 
    { 
        'foo'     : 'bar', 
        'alpha'   : 'beta', 
        'long key': 'bla' 
    } 
 
    # 93457823 more dictionaries 
] 

注意对齐的 :,以及适合人类使用的缩进。

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

请您参考如下方法:

Python 中定义了一个 PEP8 标准,用于规定代码的格式。还有一个工具可以强制执行此标准“pep8”:

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

摘自文档如下乱码:

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) 

转换为:

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)