如何遍历字典列表并将值提取到单个字符串?

时间:2021-07-30 16:09:33

标签: python

对python非常陌生。我必须提取这些值并将它们编译为单个字符串以在文本文件中使用。

列表如下所示:

    {
        "1626296627": "ALPHA: Client/Contact Communication entry\n\n"
    },
    {
        "1626296657": "BETA: Patient Communication entry\n\n"
    },
    {
        "1626296802": "CHARLIE: Clincial Record Communication button entry\n\n"
    },
    {
        "1626296835": "DELTA: Clinical Record Communication tab communication entry\n\n"
    }

2 个答案:

答案 0 :(得分:0)

这可以工作:

lst = [{
        "1626296627": "ALPHA: Client/Contact Communication entry\n\n"
    },
    {
        "1626296657": "BETA: Patient Communication entry\n\n"
    },
    {
        "1626296802": "CHARLIE: Clincial Record Communication button entry\n\n"
    },
    {
        "1626296835": "DELTA: Clinical Record Communication tab communication entry\n\n"
    }]
s=''
for d in lst:
   for key in d:
      s = s + d[key]

输出:

ALPHA: Client/Contact Communication entry

BETA: Patient Communication entry

CHARLIE: Clincial Record Communication button entry

DELTA: Clinical Record Communication tab communication entry

答案 1 :(得分:0)

所以,只需将所有内容连接在一起:

data = [{
        "1626296627": "ALPHA: Client/Contact Communication entry\n\n"
    },
    {
        "1626296657": "BETA: Patient Communication entry\n\n"
    },
    {
        "1626296802": "CHARLIE: Clincial Record Communication button entry\n\n"
    },
    {
        "1626296835": "DELTA: Clinical Record Communication tab communication entry\n\n"
    }]

output = ''.join(k+v for item in data for k,v in item.items())
print(output)

输出:

1626296627ALPHA: Client/Contact Communication entry

1626296657BETA: Patient Communication entry

1626296802CHARLIE: Clincial Record Communication button entry

1626296835DELTA: Clinical Record Communication tab communication entry