0

I have a string which looks like that (it is just an example, it has more that 1k lines):

},
series: [{x:1585937577000,y:0.1000},{x:1585937757000,y:0.1000},{x:1585937937000,y:0.1000},{x:1585938117000,y:0.0800},{x:1585938297000,y:0.0800},{x:1585938478000,y:0.0600},{x:1585939739000,y:0.0200},{x:1585939919000,y:0.0200},{x:1585940099000,y:0.0000}],
                yAxis: 0,
                color: "rgba(80,180,50,1)",

I am interested only in information in dictionaries, I want to get a list of x values and second list of y values, only that, rest is garbage. Just values without key. I've tried some regex like:

x:(.*)\,
x:\.(.*?),
x:\.(.*?),y:

However it doesn't work, propably beacuse there are no white spaces between. I am confused with any further ideas, is there easy way to do this?

2
  • 1
    Why did you add a literal dot \. here: x:\.(.*?),? None of the values of x start with a dot, and it seems to work if you remove it: regex101.com/r/My4snv/1 Commented Apr 6, 2020 at 10:51
  • Yay it solved problem, thanks bro. I always do something stupid while using regex Commented Apr 6, 2020 at 11:03

1 Answer 1

1

Not really sure what the overall structure of your data is, however if it is a list or dict of dicts on the form in your example, you could do a literal eval with the ast module and loop over the data. Note that if it is a dict of dicts, you should change the loops accordingly.

import ast

d = "[{'series': [{'x':1585937577000,'y':0.1000},{'x':1585937757000,'y':0.1000}," \
    "{'x':1585937937000,'y':0.1000},{'x':1585938117000,'y':0.0800}," \
    "{'x':1585938297000,'y':0.0800},{'x':1585938478000,'y':0.0600}," \
    "{'x':1585939739000,'y':0.0200},{'x':1585939919000,'y':0.0200}," \
    "{'x':1585940099000,'y':0.0000}], 'yAxis': 0, 'color': 'rgba(80,180,50,1)'}]"

d = ast.literal_eval(d)

x = [x['x'] for val in d for x in val['series']]
y = [x['y'] for val in d for x in val['series']]

Output:

x = [1585937577000, 1585937757000, 1585937937000, 1585938117000, 1585938297000, 1585938478000, 1585939739000, 1585939919000, 1585940099000]
y = [0.1, 0.1, 0.1, 0.08, 0.08, 0.06, 0.02, 0.02, 0.0]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.