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!