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?

More than 3 years have passed since last update.

YAMLファイル内の配列をソートしたい

Posted at

背景

flutterの静的解析の設定ファイルはYAMLで書かれています.僕の使っているその設定ファイルの中身が名前順に並んでいなかったので,ソートしようと考えたのですが,YAMLに含まれている配列までソートしてくれるツールが見つかりませんでした.なのでPythonをつかってソートをしました.本記事はそのときのメモです.

ソートしたいファイルの中身

ソートしたいyamlのリスト部分はこんな感じです
yaml.png

ツール

環境構築が面倒なのでGoogle Colabを使います

コード

# ファイルのアップロード
from google.colab import files
uploaded_file = files.upload()
uploaded_file_name = next(iter(uploaded_file))
print(uploaded_file_name)

# ソート
import yaml

with open(uploaded_file_name) as file:
    obj = yaml.safe_load(file)
    print(obj['linter']['rules'])
    obj['linter']['rules'] = sorted(obj['linter']['rules'])
    print(obj['linter']['rules'])

# 出力
output_file_name = 'analysis_options.yaml'
with open(output_file_name, 'w') as file:
    yaml.dump(obj, file)

files.download(output_file_name)

あとはエディタ上でフォーマットを整えると完成です

0
0
0

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?