What would be the best way to parse a text file that contains python dictionary?

조회 수: 3 (최근 30일)
Emmanuel
Emmanuel 2017년 3월 14일
댓글: Guillaume 2017년 3월 15일
I have a text file that contains the following dictionary. I want to plot a bar graph of these values against given keys.
  1. How can I read a file that is in dictionary format?
  2. 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.

답변 (2개)

Guillaume
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
Emmanuel
Emmanuel 2017년 3월 14일
Thank you for your answer. My matlab version is R2015a. Also, My outpout should be an array of these numbers. Ijust edited my question..sorry about that.
Guillaume
Guillaume 2017년 3월 15일
jsonlab on the FEX should allow you to decode JSON. It'll be safer than writing your own limited parser. There are several other submissions there as well (e.g. this one)
You can then easily convert whatever these output into an array.

댓글을 달려면 로그인하십시오.


Walter Roberson
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
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.
Emmanuel
Emmanuel 2017년 3월 15일
Yeah sure! Thank you very much. What I did was, I parsed it in python and converted it into an array of numbers and stored it in text file. I will definitely refer to your link as well. Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by