0

I am storing a list in python keyring. But when I retrieve it, it is converted into unicode

import keyring
c=[]
f=[]

c.append("ian")
c.append("ned")
c.append("stark")
print c
a="5555"
keyring.set_password(a,"internal",c)
d= keyring.get_password(a,"internal")
print d[0]

d=unicode: ['harish', 'ravi', 'kisan']

c=['harish', 'ravi', 'kisan'] The value of d[0] is "[" not "ian" similarly, d[1] is "i" not "ned". I want to make d as list similar to c.

imp
  • 1,967
  • 2
  • 28
  • 40

2 Answers2

2

Use ast.literal_eval. It will interpret a string as Python code, but safely.

Example:

>>> import ast
>>> l = ast.literal_eval("['hello', 'goodbye']")
>>> l
['hello', 'goodbye']
>>> type(l)
<type 'list'>

If the string you get can't be interpreted as valid Python, then you will get a ValueError. If that's the case, you'll need to show us what your output looks like in order to determine a correct solution.

2rs2ts
  • 10,662
  • 10
  • 51
  • 95
0

Use Json to parse the output:

import json
import keyring
c=[]
f=[]

c.append("ian")
c.append("ned")
c.append("stark")
print c
a="5555"
keyring.set_password(a,"internal",c)
d= json.loads(keyring.get_password(a,"internal"))
print d[0]
  • d= json.loads(keyring.get_password(a,"internal")) File "C:\Python27\lib\json\__init__.py", line 338, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 365, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 383, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded – imp Feb 13 '14 at 19:10