Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

11 views
Skip to first unread message

vanomme...@gmail.com

unread,
Jan 10, 2014, 8:57:59 PM1/10/14
to
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.

John Gordon

unread,
Jan 10, 2014, 10:53:19 PM1/10/14
to
In <aae37903-e5ab-41c5...@googlegroups.com> vanomme...@gmail.com writes:

> 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:

> n

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.

--
John Gordon Imagine what it must be like for a real medical doctor to
gor...@panix.com watch 'House', or a real serial killer to watch 'Dexter'.

Dave Angel

unread,
Jan 10, 2014, 11:12:46 PM1/10/14
to pytho...@python.org
On Fri, 10 Jan 2014 12:57:59 -0800 (PST), vanomme...@gmail.com
wrote:

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:

> n


> 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.

--
DaveA

Denis McMahon

unread,
Jan 10, 2014, 11:56:27 PM1/10/14
to
On Fri, 10 Jan 2014 12:57:59 -0800, vanommen.robert wrote:

> 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

--
Denis McMahon, denismf...@gmail.com

MRAB

unread,
Jan 11, 2014, 12:23:49 AM1/11/14
to pytho...@python.org, pytho...@python.org
[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']

vanomme...@gmail.com

unread,
Jan 11, 2014, 9:31:44 AM1/11/14
to

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.

0 new messages