Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63664 > unrolled thread
| Started by | vanommen.robert@gmail.com |
|---|---|
| First post | 2014-01-10 12:57 -0800 |
| Last post | 2014-01-11 01:31 -0800 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
Send array back in result from urllib2.urlopen(request, postData) vanommen.robert@gmail.com - 2014-01-10 12:57 -0800
Re: Send array back in result from urllib2.urlopen(request, postData) John Gordon <gordon@panix.com> - 2014-01-10 22:53 +0000
Re: Send array back in result from urllib2.urlopen(request, postData) Dave Angel <davea@davea.name> - 2014-01-10 18:12 -0500
Re: Send array back in result from urllib2.urlopen(request, postData) Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-10 23:56 +0000
Re: Send array back in result from urllib2.urlopen(request, postData) MRAB <python@mrabarnett.plus.com> - 2014-01-11 00:23 +0000
Re: Send array back in result from urllib2.urlopen(request, postData) vanommen.robert@gmail.com - 2014-01-11 01:31 -0800
| From | vanommen.robert@gmail.com |
|---|---|
| Date | 2014-01-10 12:57 -0800 |
| Subject | Send array back in result from urllib2.urlopen(request, postData) |
| Message-ID | <aae37903-e5ab-41c5-87a7-2e3e78444878@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-01-10 22:53 +0000 |
| Message-ID | <laptku$9rs$1@reader1.panix.com> |
| In reply to | #63664 |
In <aae37903-e5ab-41c5-87a7-2e3e78444878@googlegroups.com> vanommen.robert@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
gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-01-10 18:12 -0500 |
| Message-ID | <mailman.5310.1389395462.18130.python-list@python.org> |
| In reply to | #63664 |
On Fri, 10 Jan 2014 12:57:59 -0800 (PST), vanommen.robert@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
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-01-10 23:56 +0000 |
| Message-ID | <laq1bb$eu0$4@dont-email.me> |
| In reply to | #63664 |
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, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2014-01-11 00:23 +0000 |
| Message-ID | <mailman.5312.1389399830.18130.python-list@python.org> |
| In reply to | #63664 |
On 2014-01-10 20:57, vanommen.robert@gmail.com 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; > > 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']
[toc] | [prev] | [next] | [standalone]
| From | vanommen.robert@gmail.com |
|---|---|
| Date | 2014-01-11 01:31 -0800 |
| Message-ID | <04e12d2a-122c-4ed1-a517-62c34a41a2aa@googlegroups.com> |
| In reply to | #63664 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web