[Tutor] Convert SOAP response (ArrayOfInt) to Python list

Evans Anyokwu onyxtic at gmail.com
Wed Jul 10 17:01:26 CEST 2013


On Thu, Jul 4, 2013 at 8:47 PM, Robert Winkler <
robert.winkler at bioprocess.org> wrote:

>  Thanks to the OSA library, which works for SOAP requests with Python
> 3.x, I can now use SOAP services at http://www.chemspider.com.
>
> The result is a list of accession numbers (which correspond to chemical
> compounds) and I get them in the following format:
>
> (ArrayOfInt){
>     int[] = [
>              5744,
>              69182,
>              292,
>              68027,
>              3404131,
>              82616,
>              18280,
>              11200,
>              704646,
>              543430
>              ...
>              ]}
>
> How could I transform this to a simple python list?
>
> [5744, 69182, 292,68027, 3404131, 82616, 18280, 11200, 704646, 543430 ...]
>
> Conversion to a numpy array (and subsequent list(), or similar) does not
> solve the problem, since the structure is maintained; the numpy.shape
> returns ().
>
> Suggestions?
>
> Hi Robert,

You have not provided enough information. But going by the little we have
here, you could turn the returned result into a string. Then parse the
string with a regex. I have included a sample here. There are more elegant
ways to do this but here's a quick and dirty solution to give you a heads
up.


import sys
import re

retVal= '''(ArrayOfInt){
  int[] =   [
                5744,
                69182,
                292,
                68027,
                3404131,
                82616,
                18280,
                11200,
                704646,
                543430,
                ]
            }'''

myList = re.findall(r'\d+', retVal)
aList = list(myList)
print('[' + ', '.join(aList) + ']')


That should give you the desired list. Unless of course I'm missing the
point.

Good luck,
Evans
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130710/8567a266/attachment.html>


More information about the Tutor mailing list