reindent.py - 从命令行

时间:2016-03-15 11:48:46

标签: python-2.7 indentation

我在Python中有缩进问题。所以我下载了reindent.py来更正缩进错误。

我使用以下命令安装了reindent.py:

pip install reindent

但是我从命令行运行它会显示以下错误 - :

Traceback (most recent call last):
  File "/usr/local/bin/reindent", line 3, in <module>
    main()
  File "/usr/local/lib/python2.7/dist-packages/reindent.py", line 92, in main
    check(arg)
  File "/usr/local/lib/python2.7/dist-packages/reindent.py", line 118, in check
    if r.run():
  File "/usr/local/lib/python2.7/dist-packages/reindent.py", line 177, in run
    tokenize.tokenize(self.getline, self.tokeneater)
  File "/usr/lib/python2.7/tokenize.py", line 170, in tokenize
    tokenize_loop(readline, tokeneater)
  File "/usr/lib/python2.7/tokenize.py", line 176, in tokenize_loop
    for token_info in generate_tokens(readline):
  File "/usr/lib/python2.7/tokenize.py", line 357, in generate_tokens
    ("<tokenize>", lnum, pos, line))
  File "<tokenize>", line 127
    for w in transcript:
    ^
IndentationError: unindent does not match any outer indentation level

我使用以下命令运行它: -

reindent -n test1.py

我认为reindent应该纠正错误,而不是告诉我它们发生在哪里。

1 个答案:

答案 0 :(得分:0)

reindent.py将标签更改为空格,并且可以使不规则缩进成为均匀的4个空格。它不会尝试捕获或修复IndentationError

考虑这个具有IndentationError的代码:

def foo():
    print("Let's go")
  for i in range(2):   <-- IndentationError
      print('Peay')

它会向您收到的消息发出类似的错误消息:

% reindent.py script.py
Traceback (most recent call last):
  ...          
  File "/usr/lib/python2.7/tokenize.py", line 170, in tokenize
    tokenize_loop(readline, tokeneater)
  File "/usr/lib/python2.7/tokenize.py", line 176, in tokenize_loop
    for token_info in generate_tokens(readline):
  File "/usr/lib/python2.7/tokenize.py", line 357, in generate_tokens
    ("<tokenize>", lnum, pos, line))
  File "<tokenize>", line 9
    for i in range(2):
    ^
IndentationError: unindent does not match any outer indentation level

两者

def foo():
    print("Let's go")
    for i in range(2):
        print('Peay')

def foo():
    print("Let's go")
for i in range(2):
    print('Peay')

是修复代码的有效方法。 reindent.py(或tokenize模块 依赖)并不试图猜测编码器的意图。从而, IndentationErrors SyntaxError至少有时需要人类{ "data": [ { "id": "txn_17pHAkI4mX5ntfze4If5wIxW", "source": "tr_17pHAkI4mX5ntfzeIfNucubD", "amount": -100000, "currency": "usd", }, { "id": "txn_17pH21I4mX5ntfzesrhdZwyf", "source": "tr_17pH21I4mX5ntfzeKLd0SWw0", "amount": -100000, }, { "id": "txn_17pGVRI4mX5ntfzeBQPCWZZg", "source": "tr_17pGVRI4mX5ntfzegJNe1r4o", "amount": -100000, "currency": "usd" } ], }, "url": "/v1/balance/history", "count": null } 干预以解决问题。