首页 > 解决方案 > 拆分此列表的所有三个属性

问题描述

[8352] Marilyn Hudson - 16000 Views

这是我从 FacebookAdsAPI 获得的实例,我一直在尝试将单独的列表分别分成 3 个属性。

我用过 client.split("]")[1]

但它给了我Marilyn Hudson - 16000 Views。我怎样才能分别获得所有三个属性?预期产出 -

              id >> 8352
              name >> Marilyn Hudson
              impressions >> 16000

TIA

标签: djangopython-3.xsplit

解决方案


如果我理解正确,

In [1912]: client
Out[1912]: '[8352] Marilyn Hudson - 16000 Views'

In [1930]: new_list.append(re.findall(r'\d+', client.split('-')[0])[0])

In [1931]: new_list.append(client.split('-')[0].split('] ')[1].strip())

In [1932]: new_list.append(client.split('-')[1].strip())

In [1933]: new_list
Out[1933]: ['8352', 'Marilyn Hudson', '16000 Views']

让我知道这是否有帮助。


推荐阅读