So i have this piece of code here:
Outlook='https://www.myfxbook.com/api/get-community-outlook.json?session=' session_id
Outlook_response = requests.get(Outlook, verify = False)
Outlook_data = Outlook_response.json()['symbols']
I get everything i want from here but so i can f.write i need to put it into a string
lstr = ' '.join(map(str, Outlook_data))
So now im just left with this:
{'name': 'EURUSD', 'shortPercentage': 19, 'longPercentage': 81, 'shortVolume': 5241.27, 'longVolume': 22432.81, 'longPositions': 50708, 'shortPositions': 17864, 'totalPositions': 68572, 'avgShortPrice': 1.0925, 'avgLongPrice': 1.1103} {'name': 'GBPUSD', 'shortPercentage': 20, 'longPercentage': 80, 'shortVolume': 2319.04, 'longVolume': 9272.55, 'longPositions': 28637, 'shortPositions': 8572, 'totalPositions': 37209, 'avgShortPrice': 1.3014, 'avgLongPrice': 1.3248} {'name': 'USDJPY', 'shortPercentage': 79, 'longPercentage': 21, 'shortVolume': 13172.25, 'longVolume': 3475.09, 'longPositions': 4530, 'shortPositions': 23878, 'totalPositions': 28408, 'avgShortPrice': 118.2402, 'avgLongPrice': 122.0914} {'name': 'GBPJPY', 'shortPercentage': 78, 'longPercentage': 22, 'shortVolume': 1476.39, 'longVolume': 421.39, 'longPositions': 1820, 'shortPositions': 5248, 'totalPositions': 7068, 'avgShortPrice': 155.8968, 'avgLongPrice': 160.6128} etc.
My problem is that i need to filter out multiple 'name': 'xxx', 'shortVolume': xxx, 'longVolume': xxx
then i need to save them into multiple txt files. For example EURUSD.txt contains name short volume and long volume, then another file GBPUSD.txt etc.
Any help is appreciated thanks in advance
CodePudding user response:
from what I understand it, all you need is a dynamic file location that is based on name
# sample data
temp_data = [{'name': 'EURUSD', 'shortPercentage': 19, 'longPercentage': 81, 'shortVolume': 5241.27, 'longVolume': 22432.81, 'longPositions': 50708, 'shortPositions': 17864, 'totalPositions': 68572, 'avgShortPrice': 1.0925, 'avgLongPrice': 1.1103} ,{'name': 'GBPUSD', 'shortPercentage': 20, 'longPercentage': 80, 'shortVolume': 2319.04, 'longVolume': 9272.55, 'longPositions': 28637, 'shortPositions': 8572, 'totalPositions': 37209, 'avgShortPrice': 1.3014, 'avgLongPrice': 1.3248}, {'name': 'USDJPY', 'shortPercentage': 79, 'longPercentage': 21, 'shortVolume': 13172.25, 'longVolume': 3475.09, 'longPositions': 4530, 'shortPositions': 23878, 'totalPositions': 28408, 'avgShortPrice': 118.2402, 'avgLongPrice': 122.0914}, {'name': 'GBPJPY', 'shortPercentage': 78, 'longPercentage': 22, 'shortVolume': 1476.39, 'longVolume': 421.39, 'longPositions': 1820, 'shortPositions': 5248, 'totalPositions': 7068, 'avgShortPrice': 155.8968, 'avgLongPrice': 160.6128} ]
# base folder location
folder_loc = './'
for data in temp_data:
# txt contains name short volume and long volume
with open(f'{folder_loc}{data["name"]}.txt', 'w') as f:
f.write(f'{data["name"]} {data["shortVolume"]} {data["longVolume"]}')
PS: you can format the file contents and the file location as required