pythonarrayslistpython-keyring

how to retrieve list when stored in keyring in python


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.


Solution

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