将换行符添加到将分配给变量而不是“print()”的Python字符串

时间:2021-11-17 20:30:25

I'm working on a shopping list script that will input food items entered by the user into a dictionary. The script then converts those items into a string, formats the string for readability, then sends an SMS to another user via Twilio API.

我正在制作购物清单脚本,将用户输入的食品输入字典。然后,脚本将这些项转换为字符串,格式化字符串以便于阅读,然后通过Twilio API将SMS发送给另一个用户。

Everything seems to be working perfectly, except the string displays each item on the same line. I've done a search for my specific problem and, while I've seen other questions that talk about printing each element on a string to a new line, that solution does not work when I'm assigning the string to variable, which I need to do in order for Twilio to work as intended. I'd still consider myself very new to Python, so if anyone is able to help, it would be appreciated!

除了字符串显示同一行上的每个项目外,一切似乎都完美无缺。我已经搜索了我的具体问题,虽然我已经看到其他问题,谈论将字符串上的每个元素打印到一个新行,但当我将字符串赋值给变量时,该解决方案不起作用,我需要做的是让Twilio按预期工作。我仍然认为自己对Python很新,所以如果有人能够提供帮助,我们将不胜感激!

Here is the source (the 'if select == 2' section is commented out as it's not a focus I have at this time. Please disregard it):

这是源('if select == 2'部分被注释掉,因为它不是我目前关注的焦点。请忽略它):

Shoplist = {} #sets the initial empty dictionary
...
food, quantity = input("Enter the food followed by a space and the 
quantity, or type 'q q' to quit").split(); quantity
Listadd = {food: quantity} #adds the food and quantity of the food into 
a holding dictionary, Listadd
Shoplist.update(Listadd) #updates Shoplist dictionary
 for k, v in Shoplist.items(): #Concatenates dictionary into strings 
for ease of reading
    print(str(k) + ', ' + str(v)) #Shows user an updated shopping 
list
...
        Shoplist = str(Shoplist).replace("
{","").replace("}","").replace(":",",").replace("\'","")
        print(Shoplist) 

1 个解决方案

#1


0  

From the Twilio documentation:

从Twilio文档:

For sending outbound SMS messages via the REST API, it is best to encode a new line as %0a.

要通过REST API发送出站SMS消息,最好将新行编码为%0a。

With their example being:

他们的例子是:

Here+is+my+first+line%0aHere+is+my+second+line

Sounds like your mistake is in formatting the string for readability. You need to format it to the specification of the API.

听起来你的错误在于格式化字符串以提高可读性。您需要将其格式化为API的规范。

#1


0  

From the Twilio documentation:

从Twilio文档:

For sending outbound SMS messages via the REST API, it is best to encode a new line as %0a.

要通过REST API发送出站SMS消息,最好将新行编码为%0a。

With their example being:

他们的例子是:

Here+is+my+first+line%0aHere+is+my+second+line

Sounds like your mistake is in formatting the string for readability. You need to format it to the specification of the API.

听起来你的错误在于格式化字符串以提高可读性。您需要将其格式化为API的规范。