Replace quotes in a python array or arrays - python

I have a dict object in python:
'polygon': {'coordinates': [[['51.89600366973369,5.604579537426986'], ['51.88589816283178,5.598937488274174'], ['51.882074887477046,5.620585782912315'], ['51.891197214447004,5.622057621577596']]]}
I want to replace the ' from the coordinates with nothing so:
'51.89600366973369,5.604579537426986' becomes 51.89600366973369,5.604579537426986
How can I achieve that in python? To be honest, I don't now where to start.

Yes now it's indeed an array with a string with geographic coordinates, but elasticsearch want the according the geojson format (https://www.rfc-editor.org/rfc/rfc7946) which is not a string. The python function needs tranform the data and then stream it to elasticsearch.

Related

ElasticSearch. How to pass array to the search template

I have a search system in which i need to score by location. User sends his coordinates, I check whether they intersect with pre-indexed polygons. The problem is I don't know how to pass coordinates to json file. I use python library called elasticsearch.Here is sample of how I pass search data to search template
And this is how I'm trying to use it in search template file
But the problem is it gets parsed as a string, but I need an array of floats.
For coordinates,use this format : "coordinates" : [[-77.03653, 38.897676], [-77.009051, 38.889939]]
https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html
For the score please join us exemple and what you expect, but pretty sure you will have to use a https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

Convert Points Datatype of vtkUnstructuredGrid

I have an unstructured grid where the points are given as doubles. I'd like to convert them to floats.
What's the easiest way to do this?
So far I've been resorting to the hacky method of writing the grid to a legacy vtk ascii file and simply exchanging double for float in the header...
You could just recreate a dataset manually, or use a vtkArrayCalculator Filter for this task, with the following parameters :
Coordinate Results : ON
Function : "coords"
Result Array Type : Float
If you used ParaView, it would look like this :

How to define multidimensional matrix with strings in Python?

I wish to store strings in multidimensional array. I tried using numpy package along with following line:
co_entity = np.zeros((5000,4))
However, I need to store strings later on. This matrix cannot be used to store strings as it has floats/int. I tried using list to store the strings but since the number of input is dynamic, I have to use multidimensional array with upper limit.
Any ideas for this?
You could try object type with empty() function like so
co_entity = np.empty((5000,4), dtype='object')
This will allow you to store a string in each of the elements generated.

Convert string representation of list of objects back to list in python

I have a list of objects, that has been stringified:
u'[<object: objstuff1, objstuff2>, <object: objstuff1, objstuff2>]'
I want to convert this back into a list:
[<object: objstuff1, objstuff2>, <object: objstuff1, objstuff2>]
I've tried using ast.literal_eval(), but unfortunately, it doesn't seem to work if the elements are objects, and I get a SyntaxError.
Is there any way I can reconvert my string representation of the list of objects back into a list?
You need to have a look at the pickle module to do this.
Basically, dump your objects using pickle.dumps, and load them back using pickle.loads.
ast.literal_eval doesn't work obviously, because there is a lot of information related to the objects (like attributes, and values) which is simply not captured in that string. Also note that you will be able to resurrect only the pickled data, if all you have are those string representations right now, you won't be able to create the objects back from them because of the information loss.

Import matrix from text file using python

I have two text files that have matrices written in them(not numpy matrices, so its a list of lists). These matrices are written in string format, so the text file looks like this :
[[1,2,3],[3,4,5],[6,7,8]],[[3,3,3],[5,6,7],.....
I want to read this matrix back from the text file using python. I can't read using numpy as it gives ValueError: could not convert string to float
Is there anyway to do this? Would it be easier if I just wrote the matrix as a numpy matrix in the first place(I need to change code of a previous program for that, and was just wondering if there was a python way of loading matrices when it was stored as a string in a text file)?
You could make use of the ast module:
import ast
strArray = "[[1,2,3],[3,4,5],[6,7,8]]"
# evaluates the array in string format and converts it to a python array object
array = ast.literal_eval(strArray)
note:
For multiple nested arrays like you have, literal_eval will most likely convert the string into a tuple with nested arrays as elements. Just keep that in mind as you use this module.

Categories