【问题标题】:IBM Watson Assistant: How to display/print dictionary( python3) in XML syntax?IBM Watson Assistant:如何以 XML 语法显示/打印字典(python3)?
【发布时间】:2021-05-02 09:48:01
【问题描述】:

我想在 IBM watson 中显示一些 Python 字典的上下文作为输出。 例如,如果我有这样的字典

{'author': 'Bloomberg News',
 'title': 'India Crisis Worsens; N.Y. Deaths at 5-Month Low: Virus Update',
 'description': 'India Crisis Worsens; N.Y. Deaths at 5-Month Low: Virus Update',
 'url': 'https://www.bloombergquint.com/business/n-y-deaths-at-5-month-low-india-crisis-deepens-virus-update',
 'source': 'Bloomberg | Latest And Live Business',
 'image': None,
 'category': 'business',
 'language': 'en',
 'country': 'us',
 'published_at': '2021-05-02T04:48:26+00:00'}

然后在 IBM Watson Assistant 中打印标题作为输出,我们需要这样写:

<?data['title']?>

这将给出输出:India Crisis Worsens;纽约州死亡人数处于 5 个月低点:病毒更新

如果我有这样的字典:

[{'author': 'BQ Desk',
  'title': 'Coronavirus Updates: India Reports 3.9 Lakh New Covid Cases, 3,689 Deaths',
  'description': 'Coronavirus Updates: India Reports 3.9 Lakh New Covid Cases, 3,689 Deaths',
  'url': 'https://www.bloombergquint.com/coronavirus-outbreak/coronavirus-updates-india-reports-39-lakh-new-covid-cases-3689-deaths',
  'source': 'Bloomberg | Latest And Live Business',
  'image': None,
  'category': 'business',
  'language': 'en',
  'country': 'us',
  'published_at': '2021-05-02T05:15:34+00:00'},
 {'author': None,
  'title': 'Leasebak is geen heilige koe meer',
  'description': 'Managers met een leasebak in je arbeidsvoorwaarden, de vanzelfsprekendheid van de auto van de zaak gaat er vanaf. Als je nu solliciteert, zijn er steeds minder bedrijven die daar zomaar in willen meegaan. Door corona en thuiswerken nemen bedrijven hun mobiliteit nog eens extra onder de loep, zien leaseorganisaties en werkgeversorganisatie AWVN.',
  'url': 'https://www.telegraaf.nl/financieel/1632068442/leasebak-is-geen-heilige-koe-meer',
  'source': 'De Telegraaf - Financieel Overzicht',
  'image': 'https://www.telegraaf.nl/images/1200x630/filters:format(jpeg):quality(80)/cdn-kiosk-api.telegraaf.nl/a64d720a-a9c6-11eb-83a5-02d1dbdc35d1.jpg',
  'category': 'business',
  'language': 'nl',
  'country': 'nl',
  'published_at': '2021-05-02T05:00:00+00:00'},
 {'author': 'Bloomberg News',
  'title': 'India Crisis Worsens; N.Y. Deaths at 5-Month Low: Virus Update',
  'description': 'India Crisis Worsens; N.Y. Deaths at 5-Month Low: Virus Update',
  'url': 'https://www.bloombergquint.com/business/n-y-deaths-at-5-month-low-india-crisis-deepens-virus-update',
  'source': 'Bloomberg | Latest And Live Business',
  'image': None,
  'category': 'business',
  'language': 'en',
  'country': 'us',
  'published_at': '2021-05-02T04:48:26+00:00'},
 {'author': 'FE Online',
  'title': 'Coronavirus in India Live News: PM Modi to meet experts today to review oxygen & medicine availability; India reports 3.92 lakh Covid-19 cases',
  'description': 'Coronavirus Daily Cases and Deaths in India, Covid-19 India Lockdown, May 1 Phase 3 vaccine drive, Day 2 vaccination drive for 18+ years, CoWIN Covid-19 Vaccination Registration, May 2: Prime Minister Narendra Modi is expected to review the human resource situation, in relation to the Covid-19 pandemic, and ways to augment it.',
  'url': 'https://www.financialexpress.com/lifestyle/health/coronavirus-lockdown-in-india-coronavirus-cases-in-india-today-live-covid-19-cowin-vaccine-registration-vaccination-drive-rollout-coronavirus-daily-cases-and-deaths-in-india-covid-19-lockdown-in-del-2/2244047/',
  'source': 'The Financial Express',
  'image': 'https://images.financialexpress.com/2021/05/covid-second-wave.jpg',
  'category': 'business',
  'language': 'en',
  'country': 'us',
  'published_at': '2021-05-02T04:28:25+00:00'},
 {'author': 'Uppsala University',
  'title': 'Newly Discovered Immune Cell Function Vital to Healing May Lead to Treatments for Cancer and Cardiovascular Diseases',
  'description': 'Cardiovascular disease, the most common cause of death, is the result of oxygen deprivation as blood perfusion to affected tissue is prevented. To halt the...The post Newly Discovered Immune Cell Function Vital to Healing May Lead to Treatments for Cancer and Cardiovascular Diseases appeared first on SciTechDaily.',
  'url': 'https://scitechdaily.com/newly-discovered-immune-cell-function-vital-to-healing-may-lead-to-treatments-for-cancer-and-cardiovascular-diseases/',
  'source': 'Scitechdaily | Science Space And Technology News 2017',
  'image': None,
  'category': 'science',
  'language': 'en',
  'country': 'us',
  'published_at': '2021-05-01T22:40:45+00:00'}]

那么如何在XML中打印每篇文章的标题名称(如我上面提到的语法)。

Desired Output:
Coronavirus Updates: India Reports 3.9 Lakh New Covid Cases, 3,689 Deaths
Leasebak is geen heilige koe meer
India Crisis Worsens; N.Y. Deaths at 5-Month Low: Virus Update
Coronavirus in India Live News: PM Modi to meet experts today to review oxygen & medicine availability; India reports 3.92 lakh Covid-19 cases
Newly Discovered Immune Cell Function Vital to Healing May Lead to Treatments for Cancer and Cardiovascular Diseases

【问题讨论】:

  • 我删除了 Python 标签。我假设这是在 Watson Assistant 中,而不是使用 Python SDK 或 Python 客户端,对吗?

标签: xml ibm-cloud ibm-watson watson-assistant


【解决方案1】:

查看您的要求,您可能希望查看可应用于数据的方法列表,请参阅Expression language methods within the Watson Assistant Documentation。使用这些方法,您可以将数组操作为您需要的输出。

或者,您可以调用外部 REST api 来操作数据并返回所需的输出。此外部 API 可以是 IBM Cloud 函数。

最后也是最简单的,您可以拥有一个对话节点,其中包含多个响应条件,这些条件对应于答案数组的大小(python 中的列表/目录)。但是,这种方法通常只适用于小答案组,并且知道数组/列表/目录的最大大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-28
    • 2019-07-27
    • 2021-04-23
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多