Send array back in result from urllib2.urlopen(request, postData)

V

vanommen.robert

Hello,

I have a Raspberry Pi with 10 temperature sensors. I send the data from the sensors and some other values with json encoding and:

result = urllib2.urlopen(request, postData)

to a online PHP script wich places the data in a mysql database.

In the result:

result.read()

i am trying to send data back from the PHP to the RPI. I make an array in PHP

$para[0] = $REGELING_ORG;
$para[1] = $VLVERWL_ORG;
$para[2] = $VLOERVRAAG_ORG;
$para[3] = $TIJDVLOER_ORG;
$para[4] = $SETPOINT_ORG;

echo $para;

In python when i do

para = result.read()
print para

the output is:

[null,null,null,null,null,"J"]

This is correct according to the data in PHP from the mysql.

when I do

print para[1]

the output is:

n

the seccond character from the data. Why is this not the seccond datafield?
And why is para[5] not "J" but , ?

How can I change the data back to an array? I've tried with json, but that doesn't change anything.

Thanks in advance for the kind reactions!

Greetings Robert.
 
J

John Gordon

In said:
result = urllib2.urlopen(request, postData)
para = result.read()
print para
the output is:
[null,null,null,null,null,"J"]

print para[1]
the output is:

Probably because para is a string with the value
'[null,null,null,null,null,"J"]'
How can I change the data back to an array? I've tried with json, but
that doesn't change anything.

As far as I know, result.read() only returns text. If you want your results
in some other format (like an array), you'll need to parse the string.

This is a very simple (and ugly) way to do it, but it may give you a
starting point:

# open the url
result = urllib2.urlopen(request, postData)

# read the raw text results
raw_text = result.read()

# strip off the leading '[' and trailing ']'
raw_text = raw_text[1:-1]

# split raw_text into an array of strings
text_array = raw_text.split(',')

# declare a new list for storing the parsed items
para = []

# process each string
for item in text_array:
if item == 'null':
para.append(None)
else:
para.append(item)

The python csv module might have a better way to do this; have a look.
 
D

Dave Angel

No idea about the php..
In python when i do
para = result.read()
print para
the output is:
[null,null,null,null,null,"J"]

That's a string that just looks like a list.
This is correct according to the data in PHP from the mysql.

when I do
print para[1]
the output is:


the seccond character from the data. Why is this not the seccond
datafield?

There are no data fields in a string.
And why is para[5] not "J" but , ?

That's character 5 of the string.
How can I change the data back to an array? I've tried with json,
but that doesn't change anything.

You have to parse it. I don't know what rules you used at the php
end, but at a guess, I'd start by stripping the brackets, then
splitting by comma. Then iterate through each item looking for
special cases. Each item consisting of null gets replaced by None,
each item starting with quotes gets them stripped, and perhaps
anything else is replaced by float (item).

Still questions to ask like whether quoted item can have embedded
comma.
 
D

Denis McMahon

Hello,

I have a Raspberry Pi with 10 temperature sensors. I send the data from
the sensors and some other values with json encoding and:

result = urllib2.urlopen(request, postData)

to a online PHP script wich places the data in a mysql database.

In the result:

result.read()

i am trying to send data back from the PHP to the RPI. I make an array
in PHP

$para[0] = $REGELING_ORG;
$para[1] = $VLVERWL_ORG;
$para[2] = $VLOERVRAAG_ORG;
$para[3] = $TIJDVLOER_ORG;
$para[4] = $SETPOINT_ORG;

echo $para;

This is php code that prints out a string representation of the variable,
in so far as it can.

What you probably want to do is encode the array somehow, such as one
element value per line, or json encode, or some other method, and then
decode it in your python.
In python when i do

para = result.read()
print para

the output is:

[null,null,null,null,null,"J"]

Yep, that's because para is a string containing the text:

'[null,null,null,null,null,"J"]'
This is correct according to the data in PHP from the mysql.

when I do

print para[1]

the output is:

n

the seccond character from the data. Why is this not the seccond
datafield?
And why is para[5] not "J" but , ?

This is because python is looking at a string containing the character
sequence '[null,null,null,null,null,"J"]'

para[0] = '['
para[1] = 'n'
para[2] = 'u'
para[3] = 'l'
para[4] = 'l'
para[5] = ','
para[6] = 'n'
para[7] = 'u'
How can I change the data back to an array? I've tried with json, but
that doesn't change anything.

To use json to convert it back to an array in the python code, you also
need to use json to serialise the array in the php code.

eg in the php:

echo $para;

would become:

echo php_json_encoding_function( para );

and in the python:

para = result.read()

would become:

para = python_json_decoding_function( result.read() )

or possibly even:

para = json.decode( result.read() )

(I don't know the details, I'm trying to give you the general idea so you
can work out where to look to figure this out)

These two web pages may also help:

http://uk3.php.net/manual/en/function.json-encode.php
http://docs.python.org/2/library/json.html
 
M

MRAB

Hello,

I have a Raspberry Pi with 10 temperature sensors. I send the data from the sensors and some other values with json encoding and:

result = urllib2.urlopen(request, postData)

to a online PHP script wich places the data in a mysql database.

In the result:

result.read()

i am trying to send data back from the PHP to the RPI. I make an array in PHP

$para[0] = $REGELING_ORG;
$para[1] = $VLVERWL_ORG;
$para[2] = $VLOERVRAAG_ORG;
$para[3] = $TIJDVLOER_ORG;
$para[4] = $SETPOINT_ORG;

echo $para;

In python when i do

para = result.read()
print para

the output is:

[null,null,null,null,null,"J"]

This is correct according to the data in PHP from the mysql.

when I do

print para[1]

the output is:

n

the seccond character from the data. Why is this not the seccond datafield?
And why is para[5] not "J" but , ?

How can I change the data back to an array? I've tried with json, but that doesn't change anything.
[snip]

What exactly do you mean by "json doesn't change anything"? I get this:
para = '[null,null,null,null,null,"J"]'
print para [null,null,null,null,null,"J"]
import json
print json.loads(para)
[None, None, None, None, None, u'J']
 
V

vanommen.robert

I understand the problem now. the echo is a string, wich can contain text but no array.

I've changed the PHP script so I get only text separated with comma's and in python I separate the textfields and declare them in the array. With the split methode I saw in the answer of J. Gordon. Thank you for that.

@MRAB
When I encode the data in PHP and send it to Python, the results where the same.

Thanks everyone for the answers!

Greetings Robert.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top