How to transform an api query saved with ElasticSearch into a text variable?

1

I want to transform the following result of a query into an API saved with ElasticSearch in a text to be used later.

b'{"BTC":{"USD":2403.99},"ETH":{"USD":222.1},"DASH":{"USD":179.04}}'
b'{"BTC":{"USD":2402.89},"ETH":{"USD":222.1},"DASH":{"USD":179.04}}'
...

I want to get it in a variable:

json_text = """
[
{"BTC":{"USD":2403.99},"ETH":{"USD":222.1},"DASH":{"USD":179.04}},
{"BTC":{"USD":2402.89},"ETH":{"USD":222.1},"DASH":{"USD":179.04}}
]
"""

In fact it will be in the following dataframe for financial calculation:

a = json.loads(json_text)

values = [(each["BTC"].get("USD"), each["ETH"].get("USD"), each["DASH"].get("USD")) for each in a]

However, if they have a different approach, of course I am interested in listening to them

output production and safeguard attempt

Here was how I produced the output and I try to save it in a text file with good form.
In the file Main.py , I call a class Util with ut = Util

In this class I try to transform the result into a text:

import threading
from Elastic import Elastic
import urllib.request
from ValueAnalyse import ValueAnalyse 
class Util:
    def __init__(self):
        pass
    def disp(self,el,call,prices,assets):
        threading.Timer(1, self.disp,[el,call,prices,assets]).start()
        value = urllib.request.urlopen(call).read()
        prices.append(value)
        print(len(prices))
        print("prices : ")
        print(type(prices))
        a = " ".join(str(x) for x in prices)
        print(a)
        va = ValueAnalyse(a)
        el.store(assets,value)
    
asked by ThePassenger 13.07.2017 в 02:55
source

1 answer

1

So you show your input, by the use of the literal b' seems to be one or more type objects Bytes received after the invocation of an API, and what you want is to transform all in a valid JSON string that can be interpreted and converted to a dictionary. In case these objects you have them in a list, for example like this:

elastic_data = [b'{"BTC":{"USD":2403.99},"ETH":{"USD":222.1},"DASH":{"USD":179.04}}',
                b'{"BTC":{"USD":2402.89},"ETH":{"USD":222.1},"DASH":{"USD":179.04}}'
                ]

To convert everything into a single JSON string, you should do the following:

json_text = "[" + ','.join([e.decode("utf-8") for e in elastic_data]) + ']'

That is:

  • transform each Bytes into String using the appropriate coding, for example we use utf-8 but that should be consulted in the API documentation
  • We concatenate all the elements by adding a , to separate them
  • Concatenate opening and closing list [ and ]

As it was the variable json_text is valid to import it

import json
a = json.loads(json_text)
values = [(each["BTC"].get("USD"), each["ETH"].get("USD"), each["DASH"].get("USD")) for each in a]
print(values)

The exit:

[(2403.99, 222.1, 179.04), (2402.89, 222.1, 179.04)]

I hope it's useful for you

    
answered by 13.07.2017 / 19:40
source