Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
Emmanuel
Emmanuel le 14 Mar 2017
Commenté : Guillaume le 15 Mar 2017
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.

Réponses (2)

Guillaume
Guillaume le 14 Mar 2017
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 commentaires
Emmanuel
Emmanuel le 14 Mar 2017
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 le 15 Mar 2017
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.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 14 Mar 2017
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 commentaires
Walter Roberson
Walter Roberson le 15 Mar 2017
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 le 15 Mar 2017
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!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by