Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split string
#1
Hello! First post!

I'm super new to python and I'm having the hardest time trying to split this below output:

I want:

Label Date Value Close
EURUSD 1534304401 55.83 1.1324
EURUSD 1534305601 55.82 1.1323

OR

Date Value Close
1534304401 55.83 1.1324
1534305601 55.82 1.1323



{'data': {'EUR_USD': {'label': 'EUR/USD', 'data': [[1534304401, 55.83, 1.1324], [1534305601, 55.82, 1.1323], [1534306801, 56.09, 1.1328], [1534308000, 55.97, 1.1323], [1534309201, 55.91, 1.1326], [1534310401, 55.55, 1.1318], [1534311601, 55.74, 1.133], [1534312801, 55.77, 1.1329], [1534314001, 55.97, 1.1334], [1534315200, 55.83, 1.1334], [1534316400, 55.6, 1.134], [1534317600, 55.58, 1.1341], [1534318800, 55.61, 1.1338], [1534320001, 55.26, 1.1332], [1534321201, 55.24, 1.1329], [1534322400, 55.34, 1.1321], [1534323601, 55.32, 1.1326], [1534324801, 55.21, 1.1327], [1534326000, 55.19, 1.1326], [1534327200, 55.52, 1.1324], [1534328400, 55.3, 1.1327], [1534329600, 55.06, 1.132], [1534330800, 54.5, 1.1319], [1534332001, 54.41, 1.1315], [1534333201, 54.56, 1.1317], [1534334401, 54.67, 1.132], [1534335601, 54.69, 1.1326], [1534336801, 55.31, 1.1324], [1534338001, 55.36, 1.1316], [1534339201, 55.6, 1.1312], [1534340401, 55.62, 1.1314], [1534341601, 55.58, 1.1303], [1534342801, 56.55, 1.1318], [1534344001, 56.5, 1.1323], [1534345201, 56.21, 1.1312], [1534346400, 57.8, 1.1328], [1534347600, 58.1, 1.1319], [1534348801, 58.2, 1.1331], [1534350001, 58.24, 1.1338], [1534351201, 58.56, 1.1347], [1534352401, 58.45, 1.1344], [1534353601, 58.32, 1.1345], [1534354800, 58.76, 1.1343], [1534356001, 58.61, 1.1337], [1534357201, 58.57, 1.1341], [1534358401, 58.66, 1.1347], [1534359601, 58.59, 1.1347], [1534360801, 58.14, 1.135], [1534362000, 58.07, 1.1349], [1534363201, 57.89, 1.1344], [1534364401, 57.84, 1.1352], [1534365601, 57.98, 1.1352], [1534366801, 58.2, 1.1346], [1534368001, 58.16, 1.1348], [1534369200, 58.15, 1.1346], [1534370400, 58.29, 1.1346], [1534371600, 58.23, 1.1346], [1534372801, 57.98, 1.1347], [1534374000, 57.91, 1.1345], [1534375201, 57.93, 1.1342], [1534376401, 57.73, 1.1342], [1534377601, 57.52, 1.1342], [1534378800, 57.41, 1.1339], [1534380001, 57.51, 1.1341], [1534381201, 57.34, 1.1344], [1534382400, 57.66, 1.1347], [1534383601, 59.39, 1.1369], [1534384801, 59.21, 1.137], [1534386000, 58.88, 1.1374], [1534387200, 58.98, 1.1375], [1534388401, 59.02, 1.1375], [1534389600, 58.86, 1.1373]]}}}
Reply
#2
What have you tried? Post your attempt at solving the task (in Python code tags) and errors if you get them (in error tags). You can find help on that here.
We can help you correct mistakes and point you in right direction, but not write the code for you.
If you are working with JSON, make use of Python's JSON module.
Reply
#3
(Aug-16-2018, 05:06 AM)j.crater Wrote: What have you tried? Post your attempt at solving the task (in Python code tags) and errors if you get them (in error tags). You can find help on that here.
We can help you correct mistakes and point you in right direction, but not write the code for you.
If you are working with JSON, make use of Python's JSON module.
Figured it out!

position_data = output['data'][Symbol]['data']
for p in range(len(position_data)):
position_data[p][0] = datetime.datetime.fromtimestamp(position_data[p][0])
headers = ["timestamp", "lpr", "rate"]
df = pd.DataFrame(position_data, columns=headers)
df = df.set_index(df['timestamp'])
df = df.drop(['timestamp', 'rate'], axis=1)
df = df[:-1]
Reply
#4
import subprocess, os, csv
import json, http.client, math, calendar, datetime, requests
from time import *
import pandas as pd
import sys
import openpyxl
from openpyxl.reader.excel import load_workbook
import oandapyV20
import oandapyV20.endpoints.forexlabs as labs
import sched, time
s = sched.scheduler(time.time, time.sleep)
def do_something(sc): 
    print("Doing stuff...")
    Symbol='EUR_USD'
    client = oandapyV20.API(access_token="ABC-EFG")
    params ={"instrument": Symbol,"period": 3600}
    r = labs.HistoricalPositionRatios(params=params)
    output = client.request(r)
    position_data = output['data'][Symbol]['data']
    for p in range(len(position_data)):
        position_data[p][0] = datetime.datetime.fromtimestamp(position_data[p][0])
        headers = ["timestamp", "lpr", "rate"]
        df = pd.DataFrame(position_data, columns=headers)
        df = df.set_index(df['timestamp'])
        df = df.drop(['timestamp', 'rate'], axis=1)
        df = df[:-1]
    latest_value= df.tail(1)
    print(latest_value)
    # do your stuff
    s.enter(1, 1, do_something, (sc,))
s.enter(1, 1, do_something, (s,))
s.run()
I'm trying to run this chunk of code automatically every minute.

It's printing "doing something.."
but everything after it is not being run. Why? If I run the large chunk of code without the do_something.. it works.. not sure why it's not running inside of do_something
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to split string SriRajesh 5 4,186 Jun-11-2018, 02:29 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020