如何在 python 中将我的变量附加(+=)另一个变量之前添加一个新行?

How do I add a new line before appending(+=) my variable with another variable in python?

提问人:John89 提问时间:4/9/2021 最后编辑:John89 更新时间:4/9/2021 访问量:54

问:

我使用 Rasa X 作为我的前端。我看不出这与它有什么关系,但我想我会提到它以防万一。无论如何,我有一个变量正在从我的 excel 工作表中收集单元格值。我正在使用循环语句将值添加到我的变量“requested_information”中。循环完成后,我将字符串“\n”和变量“missing_info”添加到其中。打印requested_information时,新行不在那里。以下是文本结果:

enter image description here

这是我的代码:

for column in load_data.column_names:
        project_name_cell = str(sheet["B{}".format(row)].value)
        main_cell = str(sheet["" + column + "{}".format(row)].value)
        if project_id_provided == False:
            requested_information += "The " + load_data.column_names[column] + " for project " + project_name_cell + " is " + main_cell + ".\n"
        else:
            if main_cell == "n/a" or main_cell == None:
                missing_info_list.append(load_data.column_names[column])
                continue
            if column == "E" or column == "G" or column == "I" or column == "J":
                requested_information += "The " + load_data.column_names[column] + " is " + phone_formatter(main_cell) + ".\n"
            elif column == "M" or column == "N" or column == "O" or column == "P":
                requested_information += "The " + load_data.column_names[column] + " is " + "${:,.2f}".format(float(main_cell)) + ".\n"
            elif column == "Q":
                requested_information += "The " + load_data.column_names[column] + " is " + "{:.2%}".format(float(str(main_cell))) + ".\n"
            elif column == "D" or column == "F" or column == "H":
                if main_cell != None or main_cell == "":
                    if rasa_actions_settings.include_email_functions == False:
                        requested_information += ("The " + load_data.column_names[column] + " is " + main_cell + ".\n")
                    #else:
                        ##NOTE - Uncomment procedures to enable personal emails being attached to the employees' contact info
                        #email = email_functions.GetEmailAddress(main_cell)
                        #firstName = main_cell.split(" ")
                        #requested_information += ("The " + load_data.column_names[column] + " is " + main_cell + " and " + firstName[0] + "'s email address is " + email + ".\n")
            elif column == "R":
                date = main_cell.split(" ")[0]
                requested_information += "The " + load_data.column_names[column] + " is " + "{}/{}/{}".format(date[5:7], date[8:], date[0:4]) + ".\n"
            else:
                requested_information += "The " + load_data.column_names[column] + " is " + main_cell + ".\n" 
        project_id_provided = True
    if len(missing_info_list) > 0:
        missing_info += " There doesn't seem to be any information for the following catagories: \n"
        first_item_used = False
        for missing_catagory in missing_info_list:
            if first_item_used == False:
                first_item_used = True
                missing_info += missing_catagory
            else:
                missing_info += ", " + missing_catagory
        if len(missing_info_list) == 1:
            missing_info = " There doesn't seem to be any information for the " + missing_info_list[0]
        requested_information += "\n" + missing_info + "." #NOTE - HERE, THE STRINGS ARE COMBINED
    dispatcher.utter_message(requested_information)
蟒蛇 rasa rasa-x

评论

2赞 jasonharper 4/9/2021
您在 Rasa 中使用了什么确切的输出通道?例如,如果它是 HTML 格式的东西,换行符没有特定的含义,您可能需要使用一些标签,例如实际获取换行符。<br>
0赞 John89 4/9/2021
哦,你可能是对的。我会很快测试它。
0赞 John89 4/9/2021
这正是问题所在。非常感谢!如果您将其添加为答案,我将将其作为答案进行评论。我还不能投票,因为我的代表仍然太低了。@jasonharper
0赞 John89 4/9/2021
哦,对不起。你可能需要更多关于它是什么的背景信息,哈哈。所以 Rasa X 是 HTML,所以我只需要添加 html break 即可。

答: 暂无答案