What would be the best way to parse a text file that contains python dictionary?
4 ビュー (過去 30 日間)
古いコメントを表示
I have a text file that contains the following dictionary. I want to plot a bar graph of these values against given keys.
- How can I read a file that is in dictionary format?
- What is the best way to parse it in matlab? My Matlab version is R2015b
{"left_w0": -3.0177237015197758, "left_w1": -1.568878849017334, "left_w2": 1.2164467634033205, "right_s0": -0.5514660926147461, "right_s1": 0.2688301327697754, "right_w0": -1.9677138534118654, "right_w1": 1.4768400018493653, "head_pan": -0.04525243319091797, "right_w2": 2.27834496260376, "head_nod": 0.0, "torso_t0": -12.565987104803467, "left_e0": -0.996704015789795, "left_e1": 0.7044806760314942, "left_s0": -0.4613447214294434, "left_s1": -0.6074563913085937, "right_e0": 1.8457623809143067, "right_e1": 1.5876701136474611}
I want my output to just have the arrays of these numbers.
0 件のコメント
回答 (2 件)
Guillaume
2017 年 3 月 14 日
That string is JSON, which matlab can decode since R2016b (and R2017a fixes some of the bugs) with jsondecode. This will return a structure which can be easily converted into a containers.Map, the equivalent of a dictionary:
s = '{"left_w0": -3.0177237015197758, "left_w1": -1.568878849017334, "left_w2": 1.2164467634033205, "right_s0": -0.5514660926147461, "right_s1": 0.2688301327697754, "right_w0": -1.9677138534118654, "right_w1": 1.4768400018493653, "head_pan": -0.04525243319091797, "right_w2": 2.27834496260376, "head_nod": 0.0, "torso_t0": -12.565987104803467, "left_e0": -0.996704015789795, "left_e1": 0.7044806760314942, "left_s0": -0.4613447214294434, "left_s1": -0.6074563913085937, "right_e0": 1.8457623809143067, "right_e1": 1.5876701136474611}';
kvp = jsondecode(s)
mymap = containers.Map(fieldnames(kvp), struct2cell(kvp))
2 件のコメント
Walter Roberson
2017 年 3 月 14 日
s = '{"left_w0": -3.0177237015197758, "left_w1": -1.568878849017334, "left_w2": 1.2164467634033205, "right_s0": -0.5514660926147461, "right_s1": 0.2688301327697754, "right_w0": -1.9677138534118654, "right_w1": 1.4768400018493653, "head_pan": -0.04525243319091797, "right_w2": 2.27834496260376, "head_nod": 0.0, "torso_t0": -12.565987104803467, "left_e0": -0.996704015789795, "left_e1": 0.7044806760314942, "left_s0": -0.4613447214294434, "left_s1": -0.6074563913085937, "right_e0": 1.8457623809143067, "right_e1": 1.5876701136474611}';
parts = regexp(s(2:end-1), '[,:]\s*', 'split');
str2double(parts(2:2:end))
3 件のコメント
Walter Roberson
2017 年 3 月 15 日
You did not happen to post a sample file.
I speculate that the JSON is UTF encoded; I think I have seen that on some JSON.
I refer you to https://www.mathworks.com/matlabcentral/answers/270857-how-to-read-16-bit-text-with-matlab and in particular to the link there to the source code I posted to detect UTF encoding, which is a lot of the struggle in reading an unknown file.
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!