Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63671

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

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Send array back in result from urllib2.urlopen(request, postData)
Date 2014-01-10 22:53 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <laptku$9rs$1@reader1.panix.com> (permalink)
References <aae37903-e5ab-41c5-87a7-2e3e78444878@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web