0

I have a list of STRINGS each containing line of text like following:

{"name":"polygon","all_points_x":[319,469,573,685,786,1005,1008,839,684,576,515,335,319],"all_points_y":[374,310,275,249,232,211,213,230,255,280,300,374,374]}

I would like to do something like this:

x_coordinates = []
y_coordinates = []

iterate through list:
    extract numbers
    append to x_coordinates if they belong to "all_points_x":
    otherwise append to y_coordinates if they belong to "all_points_y"

But I am not sure how to do this at all.

Amaterastis
  • 477
  • 3
  • 12

1 Answers1

1

So what you have there is actually a dictionary rather than a list of strings. Dictionaries are a nice tool in python because they allow you to work with a key to access data. For example in your dictionary the keys are "name", "all_points_x", and "all_points_y" which when called return the respective values of ["polygon"] (this is a str), [319,469,573,685,786,1005,1008,839,684,576,515,335,319] (these are all ints), and [374,310,275,249,232,211,213,230,255,280,300,374,374] (also ints). So a simple answer to your question would be...

d = {"name":"polygon","all_points_x":[319,469,573,685,786,1005,1008,839,684,576,515,335,319],"all_points_y":[374,310,275,249,232,211,213,230,255,280,300,374,374]}

x_coordinates = []
y_coordinates = []

for i in d["all_points_x"]:
    x_coordinates.append(i)

for i in d["all_points_y"]:
    y_coordinates.append(i)

print(x_coordinates)
print(y_coordinates)

EDIT: Try something like this, converting the pandas element into a dictionary. (not sure what this will do to your keys)

import pandas

data = pandas.read_csv("data.csv")
d = data.to_dict()
...

EDIT2: I think this should be all you need, assuming you want ALL the x's and y's together.

import pandas
import json

x_coordinates = []
y_coordinates = []

data = pandas.read_csv("C://Users//Zak//Documents//Testing//dataset.csv")
data2 = list(data["region_shape_attributes"].values)

for i in data2:
    d = json.loads(i)
    for x in d["all_points_x"]:
        x_coordinates.append(x)
    for y in d["all_points_y"]:
        y_coordinates.append(y)
Zak M.
  • 186
  • 6
  • Thanks, but I am having a problem that I have read this from CSV file like: data = pandas.read_csv("data.csv") and then d = list(data["region_shape_attributes"].values), so I would get: TypeError: list indices must be integers, not str running the code you've just written. – Amaterastis Jul 27 '19 at 15:13
  • Sorry I see now that you mentioned it was coming from a csv file. Any chance I could peak at it or a trimmed down version of the csv file? – Zak M. Jul 27 '19 at 15:18
  • Yeah, it was in comment so my question was probably not the best. – Amaterastis Jul 27 '19 at 19:51
  • I've uploaded the file here: https://anonfile.com/U2Cao610n8/dataset_csv Thanks for the help! :) – Amaterastis Jul 27 '19 at 19:52