【问题标题】:Merge multiple lines from for loop into one list将 for 循环中的多行合并到一个列表中
【发布时间】:2019-06-27 17:11:11
【问题描述】:

我的脚本读取并解析了一个 JSON 文件:

{
"messages": 
[
    {"timestamp": "123456789", "timestampIso": "2019-06-26 09:51:00", "agentId": "2001-100001", "skillId": "2001-20000", "agentText": "That customer was great"},
    {"timestamp": "123456789", "timestampIso": "2019-06-26 09:55:00", "agentId": "2001-100001", "skillId": "2001-20001", "agentText": "That customer was stupid\nI hope they don't phone back"},
    {"timestamp": "123456789", "timestampIso": "2019-06-26 09:57:00", "agentId": "2001-100001", "skillId": "2001-20002", "agentText": "Line number 3"},
    {"timestamp": "123456789", "timestampIso": "2019-06-26 09:59:00", "agentId": "2001-100001", "skillId": "2001-20003", "agentText": ""}
]
}

我有一个 Python 脚本,它去除了“agentText”,for 循环逐行打印出每个对象:

import json

with open('20190626-101200-text-messages.json') as f:
  data = json.load(f)

for message in data['messages']:
    splittext= message['agentText'].strip().replace('\n',' ').replace('\r',' ')
    if len(splittext) > 0:
       print(splittext)

这给了我:

That customer was great
That customer was stupid I hope they don't phone back
Line number 3

我需要将这些单独的行附加在一起,所以它只是显示:

That customer was great That customer was stupid I hope they don't phone back Line number 3

所以我可以对它应用一些停用词/nltk。如何做到这一点?

【问题讨论】:

    标签: python json python-3.x list


    【解决方案1】:

    您可以将所有行连接到单个字符串变量:

    res = ""
    for message in data['messages']:
        splittext= message['agentText'].strip().replace('\n',' ').replace('\r',' ')
        if len(splittext)>0:
           res += splittext + " "
    

    或者在列表的帮助下使用字符串方法:

    res = []
    for message in data['messages']:
        splittext= message['agentText'].strip().replace('\n',' ').replace('\r',' ')
        if len(splittext)>0:
           res.append(splittext)
    print(" ".join(res))
    

    【讨论】:

      【解决方案2】:

      str.joinstr.splitlines 使用理解

      例如:

      data = {
      "messages": 
      [
          {"timestamp": "123456789", "timestampIso": "2019-06-26 09:51:00", "agentId": "2001-100001", "skillId": "2001-20000", "agentText": "That customer was great"},
          {"timestamp": "123456789", "timestampIso": "2019-06-26 09:55:00", "agentId": "2001-100001", "skillId": "2001-20001", "agentText": "That customer was stupid\nI hope they don't phone back"},
          {"timestamp": "123456789", "timestampIso": "2019-06-26 09:57:00", "agentId": "2001-100001", "skillId": "2001-20002", "agentText": "Line number 3"},
          {"timestamp": "123456789", "timestampIso": "2019-06-26 09:59:00", "agentId": "2001-100001", "skillId": "2001-20003", "agentText": ""}
      ]
      }
      
      print(" ".join(j for msg in data["messages"] for j in msg["agentText"].splitlines()))
      

      输出:

      That customer was great That customer was stupid I hope they don't phone back Line number 3
      

      【讨论】:

        猜你喜欢
        • 2021-02-23
        • 2014-02-04
        • 2021-05-20
        • 1970-01-01
        • 2020-09-05
        • 2021-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多