3

The HTTP response header for 'packages_list' returns the following, which is a list looking string. How do i convert this to an actual list? I have tried typecasting the string as a list which didn't work. I am not keen on doing find and replace or strip. Once I have the list I am creating a windows forms with buttons with text for each of the items in list. Any help is appreciated

I am using IronPython 2.6 (yes, I know its old but cant move away for backward compatibility reasons)

['Admin', 'MMX_G10_Asia', 'MMX_G10_London', 'MMX_G10_Readonly', 'MMX_Credit_Readonly', 'MMX_Govies_ReadOnly']


httpConn = httplib.HTTPConnection(base_server_url)
httpConn.request("POST", urlparser.path, params)
response = httpConn.getresponse()
headers = dict(response.getheaders())
print headers['packages_list']
1
  • Thanks everyone for your help. to my disappointment IronPython 2.6 has a bug apparently and json.loads blows up and it does not have ast package in built. and I cant installl for backward compatibility reasons File "C:\Program Files (x86)\MMI_RADIA_PKG\IronPython 2.6\Lib\json_init_.py", line 307, in loads File "C:\Program Files (x86)\MMI_RADIA_PKG\IronPython 2.6\Lib\json\scanner.py", line 42, in iterscan AttributeError: 'NoneType' object has no attribute 'scanner' IronPython bug reference ironpython.codeplex.com/workitem/25787 Commented Jun 22, 2017 at 8:18

4 Answers 4

5

The simplest approach, IMHO, would be to use literal_eval:

>>> s = "['Admin', 'MMX_G10_Asia', 'MMX_G10_London', 'MMX_G10_Readonly', 'MMX_Credit_Readonly', 'MMX_Govies_ReadOnly']"
>>> s
"['Admin', 'MMX_G10_Asia', 'MMX_G10_London', 'MMX_G10_Readonly', 'MMX_Credit_Readonly', 'MMX_Govies_ReadOnly']"
>>> from ast import literal_eval
>>> literal_eval(s)
['Admin', 'MMX_G10_Asia', 'MMX_G10_London', 'MMX_G10_Readonly', 'MMX_Credit_Readonly', 'MMX_Govies_ReadOnly']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mureinik for taking time to help. Sadly there is no ast in IronPython 2.6
1

You can to check if the string is a valid python type

>>> import ast
>>> s = "['Admin', 'MMX_G10_Asia', 'MMX_G10_London', 'MMX_G10_Readonly', 'MMX_Credit_Readonly', 'MMX_Govies_ReadOnly']"
>>> ast.literal_eval(s)
['Admin', 'MMX_G10_Asia', 'MMX_G10_London', 'MMX_G10_Readonly', 'MMX_Credit_Readonly', 'MMX_Govies_ReadOnly']

4 Comments

Thanks for the answer Fred. unfortunately the stupid IronPython 2.6 does not have ast package :-(
Looks like I am using the oldest of 2.6 series ' "C:\Program Files (x86)\MMI_RADIA_PKG\IronPython 2.6\ipy.exe" C:/Users//PycharmProjects/MultiPackageSupport/temp/temp1.py Traceback (most recent call last): File "C:/Users//PycharmProjects/MultiPackageSupport/temp/temp1.py", line 1, in C:/Users//PycharmProjects/MultiPackageSupport/temp/temp1.py ImportError: No module named ast Process finished with exit code 1 '
Hello mate, I cant, it has been distributed to 100s of user desktops. so my work should work on their PCs with their version of IronPython. thanks
1

Another option is to convert this string to JSON format and then read it in:

import json
s = headers['packages_list'].replace("'", '"')
result = json.loads(s)

4 Comments

Do you really need to replace all '' with "? Doesn't JSON use either one to enclose a string literal?
@Code-Apprentice not sure what JSON specification says, but json.load only accepts " as string delimiter.
The example at docs.python.org/2/library/json.html and docs.python.org/3/library/json.html says otherwise: >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]').
Oh wait...I had it backwards. My bad.
0

Ugly but I am going with the below. Thanks again everyone for the help !

headers = dict(response.getheaders())
print headers['packages_list']
result = headers['packages_list'].replace("'",'')
result = result.replace("[","")
result = result.replace("]", "")
print result
package_list = result.split(",") 
print "the 2nd item in teh list is ", package_list[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.