2016-10-20 10 views
0

JSONテキスト文字列内の別の単語の後にのみ出現する単語の出現を置き換えようとしています。私はこれを行うために正規表現を使用するのに苦労してきましたが、ちょうどPython関数を使用することは私にとってうまくいくでしょう。そして、その後、"session_transition":の最初の発生を見つける、(引用符で)"session_transition":後に引用符で囲まれているものは何でも文字列を見つけ、その後、別の文字列に置き換えますPythonでは、別の単語の出現後に単語の最初の出現を照合する

だから私が欲しいのは"LEVEL1"の最初の発生を見つけることです。ここで私が働いている文字列です。たとえば

"BASELINE": { 
    "audio_volume": 150, 
    "cry_threshold": 70, 
    "cry_transition": "LEVEL1", 
    "expected_volume": 63, 
    "led_color": "BLUE", 
    "led_blink_speed": "NONE", 
    "motor_amplitude": 0.97, 
    "motor_frequency": 0.5, 
    "power_transition": "SUSPENDED", 
    "seconds_to_ignore_cry": 10.0, 
    "seconds_in_state": -1.0, 
    "session_transition": "ONLINE", 
    "track": "RoR", 
    "timer_transition": null, 
    "active_session" : 1 
}, 
"LEVEL1": { 
    "audio_volume": 300, 
    "cry_threshold": 75, 
    "expected_volume": 63, 
    "cry_transition": "LEVEL2", 
    "led_color": "PURPLE", 
    "led_blink_speed": "NONE", 
    "motor_amplitude": 0.76, 
    "motor_frequency": 1.20, 
    "power_transition": "SUSPENDED", 
    "seconds_to_ignore_cry": 10.0, 
    "seconds_in_state": 480.0, 
    "session_transition": "ONLINE", 
    "track": "RoR", 
    "timer_transition": "BASELINE", 
    "active_session" : 1 
} 

}

は、私が見つけて"LEVEL1":"ONLINE"を交換したい以下 - それは次のようになります"OFFLINE"へ>"session_transition":

"LEVEL1": { 
    "audio_volume": 300, 
    "cry_threshold": 75, 
    "expected_volume": 63, 
    "cry_transition": "LEVEL2", 
    "led_color": "PURPLE", 
    "led_blink_speed": "NONE", 
    "motor_amplitude": 0.76, 
    "motor_frequency": 1.20, 
    "power_transition": "SUSPENDED", 
    "seconds_to_ignore_cry": 10.0, 
    "seconds_in_state": 480.0, 
    "session_transition": "OFFLINE", 
    "track": "RoR", 
    "timer_transition": "BASELINE", 
    "active_session" : 1 
} 

これまでのところ、私はr"(?<=\"LEVEL1\"\:).*が最初の発生と一致するようになっていますが、さらに進める方法はわかりません。その後

+0

なぜこれを正規表現で実行しようとしていますか?それを解析して辞書を処理する。 – jonrsharpe

+0

@jonrsharpe:私は文字列を残して、それがあなたが意味するものであれば、dictにならないようにする必要があります。 – RebelAlliance

+0

なぜですか?後でそれを文字列に戻すことができないのですか?あなたが解決しようとしている*実際の問題は何ですか? – jonrsharpe

答えて

0

[OK]を、私は次のようにこの問題を解決することができたので:

#1)Find the unique string "LEVEL1": and save its index 
after_index = configuration_in_text_format.index('"LEVEL1":') 
#2)Starting from previous index, find the "session_transition": string and save its index 
after_index = configuration_in_text_format.find('"session_transition":', after_index) 
#3)Create a new string of bottom part with "session_transition": as first line 
new_config_in_text_format = configuration_in_text_format[after_index:] 
#4)Remove the first line with "session_transition": in it 
new_config_in_text_format = new_config_in_text_format[new_config_in_text_format.find('\n')+1:] 
#5)Create a new string to replace the deleted new line 
new_line_str = '"session_transition": "OFFLINE",\n' 
#6)Put new string on top of remaining text, effectively replacing old line 
new_config_in_text_format = new_line_str + new_config_in_text_format 
#7)Take the top part of original text and append the newly modified bottom part 
new_config_in_text_format = configuration_in_text_format[:after_index] + new_config_in_text_format 
print new_config_in_text_format 

そして、適切な出力:

"BASELINE": { 
    "audio_volume": 150, 
    "cry_threshold": 70, 
    "cry_transition": "LEVEL1", 
    "expected_volume": 63, 
    "led_color": "BLUE", 
    "led_blink_speed": "NONE", 
    "motor_amplitude": 0.97, 
    "motor_frequency": 0.5, 
    "power_transition": "SUSPENDED", 
    "seconds_to_ignore_cry": 10.0, 
    "seconds_in_state": -1.0, 
    "session_transition": "ONLINE", 
    "track": "RoR", 
    "timer_transition": null, 
    "active_session" : 1 
}, 
"LEVEL1": { 
    "audio_volume": 300, 
    "cry_threshold": 75, 
    "expected_volume": 63, 
    "cry_transition": "LEVEL2", 
    "led_color": "PURPLE", 
    "led_blink_speed": "NONE", 
    "motor_amplitude": 0.76, 
    "motor_frequency": 1.20, 
    "power_transition": "SUSPENDED", 
    "seconds_to_ignore_cry": 10.0, 
    "seconds_in_state": 480.0, 
    "session_transition": "OFFLINE", 
    "track": "RoR", 
    "timer_transition": "BASELINE", 
    "active_session" : 1 
}, 
0
私はあなたが多少簡単に(string.indexを使用して、これを行うことができると思い

first_index = some_string.index('"Level1"') 
second_index = some_string[first_index:].index('"Online"') 

私は、文字列を置換するためにあなたにそれを任せます。あなたはsome_string[second_index:].split('"')を使用してそれを行うことができますし、スプライシングと結合を使用してそれを戻すことができるはずです。

+0

"ONLINE"は私が遭遇する最初のものではありません。 LEVEL1 - > session_transitionブランチにある限り、実際には他の文字列にすることができます。だからこそ、私は "LEVEL1"と一致させようとしています:最初に "session_transition"とマッチします:その次の順です。 – RebelAlliance

+0

よろしいですか?ポイントは最初に一致させたい単語に一致する最初の行で、2番目の行は2番目に一致させる単語に一致します。 – bravosierra99

+0

ありがとう、これは私を解決に導いた。私は間違って正規表現の底なしの深淵を下った。学んだ教訓。 – RebelAlliance

0

Pythonで組み込みのJSONライブラリを使用することをお勧めします。 JSONをPython Dictオブジェクトに簡単に変換できます。これは複雑な正規表現からあなたを防ぐでしょう。読みやすさの複雑さを減らす方が良いでしょう。 ドキュメント:Pythonの2で Python 3.4 Python 2.7

import json 
jsonDict = json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') 
print(jsonDict) 

結果:あなたはPythonの辞書に行うようにあなたは、簡単に操作を行うことができます

['foo', {'bar': ['baz', None, 1.0, 2]}] 

。このように直感的に思えます。

あなたが完了したら、あなたが使用してそれを変換することができ

jsonStr = json.dumps(jsonDict) 
print(jsonStr) 

結果:

["foo", {"bar":["baz", null, 1.0, 2]}] 
+0

これは良い解決策であり、動作します。しかし、私はJSONから文字列に変換すると、データが並べ替えられます。私はこれが期待されていることを知っていますが、文字列を初期状態から順不同に変更することは望ましくありません。 – RebelAlliance

関連する問題