python初心者の頃に書いたスクリプトのインデントが、
スペースタブ混じりでスペース数タブ数もめちゃくちゃで非常に見づらい状態。
書き直したくてもその時間もなくせめてインデントを正常にして見やすくしたいと考えて
pythonスクリプトのインデントを整形するスクリプトを書こうと思い立った。
最初ChatGPTに丸投げしたがうまくいかなかったので
3時間ぐらいかけて作成してみました。
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
'''