0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonスクリプトのインデントを整理整頓するプログラム作成

Posted at

python初心者の頃に書いたスクリプトのインデントが、
スペースタブ混じりでスペース数タブ数もめちゃくちゃで非常に見づらい状態。
書き直したくてもその時間もなくせめてインデントを正常にして見やすくしたいと考えて

pythonスクリプトのインデントを整形するスクリプトを書こうと思い立った。

最初ChatGPTに丸投げしたがうまくいかなかったので
3時間ぐらいかけて作成してみました。

image.png

pyindent.py
def process_indentation(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8') as f:
        lines = f.readlines()

    indent_white = {} # 直近のインデントテキスト情報保存 1:"\t",2:"\t\t"
    indent_level = 0 # 現在のインデントレベル
    
    splitlines = [["","",0]] ## [インデントテキスト,コード,インデント数]

    rownum = 0
    for line in lines:
        rownum += 1
        stripped_line = line.lstrip()  # 行頭の空白を除いた内容部分
        leading_whitespace = line[:len(line) - len(stripped_line)]  # 行頭の空白部分
        
        print("debug",len(leading_whitespace),len(stripped_line),line)
        
        if len(stripped_line) == 0 :
            pass # スペース行
            splitlines.append(["","",0])
        elif len(leading_whitespace) == 0 :
            # インデント無し
            splitlines.append([leading_whitespace,stripped_line,0])
            indent_level = 0
        elif len(leading_whitespace) > 0 :
            if splitlines[-1][0] == leading_whitespace :
                # indentが同じ
                pass
            elif len(splitlines[-1][0]) < len(leading_whitespace) :
                # indentが増える
                indent_level += 1
                indent_white[indent_level] = leading_whitespace
            elif len(splitlines[-1][0]) > len(leading_whitespace) :
                # indentが減る
                while True :
                    indent_level -= 1
                    if indent_white[indent_level] == leading_whitespace:
                        break
                
            # インデント有
            splitlines.append([leading_whitespace,stripped_line,indent_level])

    # 結果を出力ファイルに書き込み
    with open(output_file, 'w', encoding='utf-8') as f:
        for row in splitlines :
            print(row)
            f.write(" "*4*row[2] + row[1].strip() + "\n")

# 使用例
input_file = 'pysample.txt'  # 整理対象のPythonスクリプト
output_file = 'pysample2.txt'  # 整理後のファイル
process_indentation(input_file, output_file)

注意点は下記のような複数行のテキストがプログラム内にあると正常に動作しない

py.py
def test() :
	text = '''aaa
bbb
ccc
'''
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?