Betfair Developers

A place to discuss Betfair API programming with the Python language

Post Reply
Neophyte Neophyte is offline
Junior Member
Default How do you return a list of MarketIds as a string with each record in double quotes

I am trying to convert my Javascript program to Python.

If I hard code the marketIds into an API request they work fine ie

requestFilters = {"filter":{"marketIds":["1.128390571","1.131715486"]}, "sort":"FIRST_TO_START", "marketProjection":["MARKET_START_TIME","RUNNER_DESCRIPTION"], "maxResults":"3"}

and returns the following if you print request filters.

requestFilters = {"filter":{"marketIds":["1.128390571","1.131715486"]}, "sort":"FIRST_TO_START", "marketProjection":["MARKET_START_TIME","RUNNER_DESCRIPTION"], "maxResults":"3"}

problem is I want to get the MarketIds for the next 10 markets and then get data on them. I need to be able to collect them in a list which I pass to request filters as a string. Market Ids are strings and sometimes have trailling zeros so they must be stored as strings.

If I create an array with the same markets and pass it to requestFilters as a string it fails because it converts double quotes to single quotes,
single quotes remain single quotes,
single + double quotes convert to single + double and not double as I had hoped.
Only double quotes work in requestFilters so I have to get my list to leave each item surrounded by double quotes


Markets = ["1.128390571","1.131715486"]

MarketsString = str(Markets)
print ("\nMarketsString = ", MarketsString)
requestFilters = '{"filter":{"marketIds":'+str(Markets)+'}, "sort":"FIRST_TO_START", "marketProjection":["MARKET_START_TIME","RUNNER_DESCRIPTION"], "maxResults":"3"}'
print ("\n requestFilters = ",requestFilters)


MarketsString = ['1.128390571', '1.131715486']

requestFilters = {"filter":{"marketIds":['1.128390571', '1.131715486']}, "sort":"FIRST_TO_START", "marketProjection":["MARKET_START_TIME","RUNNER_DESCRIPTION"], "maxResults":"3"}

which doesn’t work

I have spent hours on this and can find no solutions
betdynamics betdynamics is offline
Senior Member

Are you sure this isn't just a facet of the print function?

If not, you can probably:

1. Just replace all single quotes with double quotes using the replace function


2. Use json.dumps to produce a json string.

LiamP LiamP is offline
Senior Member

You are trying to build a string rather than a python dictionary which you can then convert to json using json.dumps().

For example

import json

markets = ['1.12424345', '1.1243545']
filter = {'filter': markets}

request = json.dumps(filter)
Or just use my library and save yourself a few days of pain
Neophyte Neophyte is offline
Junior Member

Thanks everyone. You are really quick. No doubt after you had posted your replies I found a solution. Which I set out below. This is essentially betDynamics first solution but it took me a little while to get it to work for the reason given at the end. I will look at json.dumps to understand that and also Liamps library.

Thank you both for the speed and helpful pointers

Markets = ["1.128390571","1.131715486"]

MarketsString = str(Markets)
MarketsString = MarketsString.replace("'",'"') # that is (double single double,single double single)
print ("\nMarketsString = ", MarketsString)
requestFilters = '{"filter":{"marketIds":'+MarketsString+'}, "sort":"FIRST_TO_START", "marketProjection":["MARKET_START_TIME","RUNNER_DESCRIPTION"], "maxResults":"3"}'
print ("\n requestFilters = ",requestFilters)

Now outputs as

MarketsString = ["1.128390571", "1.131715486"]

requestFilters = {"filter":{"marketIds":["1.128390571", "1.131715486"]}, "sort":"FIRST_TO_START", "marketProjection":["MARKET_START_TIME","RUNNER_DESCRIPTION"], "maxResults":"3"}

which works at least until I try reading data from market catalogue and adding to Markets

A word of warning. Using either ‘’’ or “”” in string.replace creates a multiline comment starter which remarks out the rest of the program
Post Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump