0

Needing some help on where to start. I have inherited a file called data.php which consist of an array shaped as follows:

<?php
error_reporting(0);
$items=array (
0 =>
array (
'name' => 'Ted',
'size' => 'Large',
'sleeve' => 'Short',
'color' => 'Red',
),

);
$unObiect->items = $items;
$json = json_encode($unObiect);
echo($json);
?>

There are actually about one hundred arrays in the file. I am trying to open the file, search an element of the array and replace a value. Not concerned about the replace part yet just trying to open the file and loop thru the array. I'm not sure how to actually open the file in Python and loop it. I have tried

with open("data.php") as raw:
    for items in raw:
        print(items)

This opens the file but seems to be reading by line which I expected. How can it be opened and loop thru as an array, something like

with open("data.php") as raw:
for items in raw['name']:
       print(items)

Where I can get an element of the array without going thru every line and seeing if that line contains 'name' then splitting the line to get to the value after the '=>'. I guess basically looking to open the file and loop the array. I know there are other post similar to this but I guess I'm not sure if I'm actually opening a php file as a text file somebody named with php or an actual php file that has an actual json array. Hope this makes sense.

Edit: From a comment I took suggestion to execute the php from the python. Using the following I was able to do this:

result = subprocess.run(
['php', 'data.php'],
stdout=subprocess.PIPE,
check=True
)
print(result.stdout)    

Not sure now what to do with the result.stdout, how to iterate thru it. This returns a byte-string.

snowman
  • 123
  • 3
  • 10
  • i don't know much about python, but maybe access the page thru http, then get the body response? im guessing you're opening the actual file itself – Kevin May 30 '19 at 01:09
  • Perhaps use regex to find the part of the file you want to edit and then replace it. Can you share what value you want to replace? – Jay May 30 '19 at 01:10
  • 1
    or prolly execute the php script thru command line. another way – Kevin May 30 '19 at 01:10
  • Opening it as text won't allow you to read the json output. Python would need to execute php with a shell and capture the json output, then parse the json. I would not recommend trying to read out the PHP array format in python. – Michael Berkowski May 30 '19 at 01:11
  • Actually the file is local and not being executed at all. It seems the data.php is just a file that was created with the php ext. – snowman May 30 '19 at 01:11
  • @Jay why would you want the OP to do that? its like opening a can of worms using regex to parse text in it. – Kevin May 30 '19 at 01:12
  • On the command line, if you ran `$ php data.php`, the output would be JSON. Make Python do the same thing itself. – Michael Berkowski May 30 '19 at 01:12
  • @Michael Berkowski-I am able to get the Python to execute the php and print(result.stdout) which prints the contents. Can the result.stdout be iterated in some manner? – snowman May 30 '19 at 01:56
  • @snowman Load `result.stdout` as a JSON string with `json.loads()` so you can use it in python as a proper data structure https://stackoverflow.com/questions/7771011/parse-json-in-python (I'm not a python programmer and can't provide the best method) – Michael Berkowski May 30 '19 at 02:37

2 Answers2

1
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import os

In [2]: with os.popen('php data.php') as p:
   ...:     s = p.read()
   ...:

In [3]: s
Out[3]: '{"items":[{"name":"Ted","size":"Large","sleeve":"Short","color":"Red"}]}'

In [4]: import json

In [5]: r = json.loads(s)

In [6]: r
Out[6]:
{'items': [{'name': 'Ted',
   'size': 'Large',
   'sleeve': 'Short',
   'color': 'Red'}]}
Waket Zheng
  • 5,065
  • 2
  • 17
  • 30
0

You can make the PHP to return the array as json above instead of echo. Then you can make a network request from python scripts to php script using request library like so:

r = requests.get('route_to_php_file')
    if r.status_code == 200:
        return r.json()

You can learn more about python request library: https://2.python-requests.org/en/master/

Emeka Augustine
  • 891
  • 1
  • 12
  • 17