如何在从字典中检索列表时创建列表



我正在使用get()函数在字典中查找一组电子邮件地址

get函数返回

['[“one.yahoo.com”,”two.yahoo.com”,"three.yahoo.com"]']

我尝试了几种方法来删除单引号,所以我最终得到一个像这样的列表

email = [ [“one.yahoo.com”,”two.yahoo.com”,"three.yahoo.com"] ]

字典是这样的

dict ={"仓库":"("x@yahoo.com"、"y@yahoo.com"、"z@yahoo.com")","灌装":"("one.yahoo.com"、"two.yahoo.com"、"three.yahoo.com"]"}

试试这个:

import ast # This contains a function to convert str to list
email = ['["one.yahoo.com", "two.yahoo.com", "three.yahoo.com"]']
email = str(email) # Converts to str type.
email = email.replace("'", "") # Replaces the apostrophes with blanks.
email = ast.literal_eval(email) # Converts to list. 
print(email)

输出是:

[["one.yahoo.com", "two.yahoo.com", "three.yahoo.com"]]

你可以试试:

email = ['["one.yahoo.com","two.yahoo.com","three.yahoo.com"]']
email = email[0].replace('"','')
email = email[1:-1].split(",")

希望对你有帮助。

相关内容

  • 没有找到相关文章

最新更新