What would be the best way to parse a text file that contains python dictionary?
조회 수: 3 (최근 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!