简体   繁体   中英

How to decode Json in python and iterate

I am new in python,I am sending a php json encoded array to python like this:

$myArray = array(1,5,8,6);
$jsonArray = json_encode($myArray);

exec("python ".$absPath."/myPythonFile.py $absPath $parm $jsonArray",$out);

Here is the python code:

from pydfs_lineup_optimizer import *
import sys
import json

optimizer = LineupOptimizer(FanDuelBasketballSettings)
optimizer.load_players_from_CSV("test_new.json")
try:
    players_that_you_want = []
    for number in sys.argv[3]:
        print number
    lineups = optimizer.optimize()
    for l in lineups:
        print(l)
except LineupOptimizerException as e:
    print(e)

When I am trying to print number variable inside python file,This is how python returning:

[
1
,
5
,
8
,
6
]

and I want it like this:

1
5
8
6

Any Idea What I am doing wrong?

The argument is being sent in as a string, so you're not actually splitting it into the proper values with your loop. You can try stripping the square brackets and using split(',') .

input = "[1,2,3,4]"
processed_input = input.strip('[]').split(',')

The above code will do the job.

This is what we need to do:

from pydfs_lineup_optimizer import *
import sys
import json

optimizer = LineupOptimizer(FanDuelBasketballSettings)
optimizer.load_players_from_CSV("test_new.json")
try:
    players_that_you_want = []
    for number in json.loads(sys.argv[3]):
        print number
    lineups = optimizer.optimize()
    for l in lineups:
        print(l)
except LineupOptimizerException as e:
    print(e)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM