pythonでデータ整理プログラムを作りたい。

実現したいこと

データの整理プログラムを開発したい。

{}の位置の対応関係を判断し、ブロックごとの固まりをテキストに出力するプログラムを作りたい。

前提

仕事で必要なデータの整理プログラムを作りたいと思っています。
同様の事をVBAで試そうかと思ったのですが、Pythonの方が正規表現があるため、目的に対して適した結果が出やすいと考えPythonを実行しようと思っています。


{
{
{
bandEUTRA-r10 1,
bandParametersUL-r10
{
{
ca-BandwidthClassUL-r10 a
}
},
bandParametersDL-r10
{
{
ca-BandwidthClassDL-r10 a,
supportedMIMO-CapabilityDL-r10 twoLayers
}
}
}
},

このような塊があるときに{と位置的に真下にある },ここまでが
データの固まりの始点と終点になるのですが、今のプログラムだと
真ん中の
ca-BandwidthClassUL-r10 a
}
},
},
ここまでが区切りであると考えて

Combination 1:
ca: a

Combination 2:
ca: a
supportedMIMO: twoLayers

2つの組み合わせが出力されるのですが、

{
{
{
bandEUTRA-r10 1,
bandParametersUL-r10
{
{
ca-BandwidthClassUL-r10 a
}
},
bandParametersDL-r10
{
{
ca-BandwidthClassDL-r10 a,
supportedMIMO-CapabilityDL-r10 twoLayers
}
}
}
},

ここまでで一つの固まりであると認識し、

bandEUTRA-r10 1,の”1”と

ca-BandwidthClassUL-r10 a
ca-BandwidthClassDL-r10 a,

この双方にあるaを組み合わせて

1A 
で下にある
supportedMIMO-CapabilityDL-r10 twoLayers

ここにある情報twoLayers と組み合わせて

1A two layrer が求めている出力となります。

もし
ca-BandwidthClassUL-r10 a
ca-BandwidthClassDL-r10 c,
とcになっている場合は1Cになるのですが、それは少しずつ拡張していきたいと
思っています。

pythonに位置を判断し、正確な出力が出るようにプログラムを修正したいのですが
どうしたらいいでしょうか?
どうしたらいいでしょうか?

発生している問題・エラーメッセージ

固まりの位置関係を把握できず、
本来であれば一塊の組み合わせを2つの組み合わせと考え出力してしまっている。

該当のソースコード

Python

1import re 2import os 3 4def extract_combinations(log):5 combinations = []6 pattern = re.compile(r'\{([^{}]+)\}', re.MULTILINE)7 8 matches = pattern.findall(log)9 for match in matches:10 combination = {}11 lines = match.strip().split('\n')12 for line in lines:13 key, value = re.findall(r'\b(\w+)\s*-\w+-\w+\s*([^,}]+)', line)[0]14 combination[key] = value.strip()15 combinations.append(combination)16 17 return combinations 18 19# 入力となるログデータ20log_data = """ 21 { 22 { 23 { 24 bandEUTRA-r10 1, 25 bandParametersUL-r10 26 { 27 { 28 ca-BandwidthClassUL-r10 a 29 } 30 }, 31 bandParametersDL-r10 32 { 33 { 34 ca-BandwidthClassDL-r10 a, 35 supportedMIMO-CapabilityDL-r10 twoLayers 36 } 37 } 38 } 39 }, 40 { 41 { 42 bandEUTRA-r10 3, 43 bandParametersUL-r10 44 { 45 { 46 ca-BandwidthClassUL-r10 a 47 } 48 }, 49 bandParametersDL-r10 50 { 51 { 52 ca-BandwidthClassDL-r10 a, 53 supportedMIMO-CapabilityDL-r10 fourLayers 54 } 55 } 56 } 57 }, 58 { 59 { 60 bandEUTRA-r10 8, 61 bandParametersUL-r10 62 { 63 { 64 ca-BandwidthClassUL-r10 a 65 } 66 }, 67 bandParametersDL-r10 68 { 69 { 70 ca-BandwidthClassDL-r10 a, 71 supportedMIMO-CapabilityDL-r10 twoLayers 72 } 73 } 74 } 75 }, 76 { 77 { 78 bandEUTRA-r10 11, 79 bandParametersUL-r10 80 { 81 { 82 ca-BandwidthClassUL-r10 a 83 } 84 }, 85 bandParametersDL-r10 86 { 87 { 88 ca-BandwidthClassDL-r10 a, 89 supportedMIMO-CapabilityDL-r10 twoLayers 90 } 91 } 92 } 93 }, 94 { 95 { 96 bandEUTRA-r10 28, 97 bandParametersUL-r10 98 { 99 { 100 ca-BandwidthClassUL-r10 a 101 } 102 }, 103 bandParametersDL-r10 104 { 105 { 106 ca-BandwidthClassDL-r10 a, 107 supportedMIMO-CapabilityDL-r10 twoLayers 108 } 109 } 110 } 111 }, 112 { 113 { 114 bandEUTRA-r10 41, 115 bandParametersUL-r10 116 { 117 { 118 ca-BandwidthClassUL-r10 a 119 } 120 }, 121 bandParametersDL-r10 122 { 123 { 124 ca-BandwidthClassDL-r10 a, 125 supportedMIMO-CapabilityDL-r10 fourLayers 126 } 127 } 128 } 129 }, 130 { 131 { 132 bandEUTRA-r10 42, 133 bandParametersUL-r10 134 { 135 { 136 ca-BandwidthClassUL-r10 a 137 } 138 }, 139 bandParametersDL-r10 140 { 141 { 142 ca-BandwidthClassDL-r10 a, 143 supportedMIMO-CapabilityDL-r10 fourLayers 144 } 145 } 146 } 147 }, 148 { 149 { 150 bandEUTRA-r10 3, 151 bandParametersUL-r10 152 { 153 { 154 ca-BandwidthClassUL-r10 a 155 } 156 }, 157 bandParametersDL-r10 158 { 159 { 160 ca-BandwidthClassDL-r10 a, 161 supportedMIMO-CapabilityDL-r10 fourLayers 162 } 163 } 164 }, 165 { 166 bandEUTRA-r10 1, 167 bandParametersDL-r10 168 { 169 { 170 ca-BandwidthClassDL-r10 a, 171 supportedMIMO-CapabilityDL-r10 twoLayers 172 } 173 } 174 } 175 }, 176"""177 178# 組み合わせを抽出179combinations = extract_combinations(log_data)180 181# 出力先のパスを取得182desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")183output_file_path = os.path.join(desktop_path, "output.txt")184 185# 結果をテキストファイルに保存186with open(output_file_path, 'w') as file:187 for i, combination in enumerate(combinations, 1):188 file.write(f"Combination {i}:\n")189 for key, value in combination.items():190 file.write(f" {key}: {value}\n")191 file.write('\n')192 193print(f"結果は {output_file_path} に保存されました。")

試したこと

正規表現の組み合わせを修正したり試行錯誤しました。

やや目標が複雑ですが、どう修正すれば良いのかわかる方いらっしゃいましたらご教示いただけませんか?

コメントを投稿

0 コメント