简体   繁体   English

将for循环的输出包含到python中的变量中

[英]Encompass the output of a for loop into a variable in python

I'm trying to define the following for loop as a variable so that it can be sent as a text message using Twilio. 我试图将以下for循环定义为变量,以便可以使用Twilio将其作为文本消息发送。 The for loop is parsing some json output. for循环正在解析一些json输出。

for item in parsed:
    print item['resource']['caseNumber'], \
    item['resource']['subject'], \
    item['resource']['status'], \
    item['resource']['severity'] 

The following code snippet produces output like: 以下代码段产生的输出如下:

123458 Subject Example Status Example 4 (Low) 
123138 Subject Example2 Status Example 3 (Normal) 
145541 Subject Example3 Status Example 2 (High)

I'm trying to encompass that entire output into a variable, is that possible? 我试图将整个输出包含到一个变量中,这可能吗? If not, what's the best way to take the output I've captured from the for loop above and then send it via Twilio with: 如果不是,最好的方法是获取我从上面的for循环中捕获的输出,然后通过Twilio使用以下命令将其发送:

# Send text messages 
client = TwilioRestClient(account_sid, auth_token)
message = client.messages.create(to=to,from_=from_,body=body)

So you could send a list of strings 所以你可以发送一个字符串列表

body=[]
for item in parsed:
    body.append(item['resource']['caseNumber'], \
        item['resource']['subject'], \
        item['resource']['status'], \
        item['resource']['severity'] )

or you could just send one long string, with newlines appended to the end of each iteration of the for-loop. 或者,您可以只发送一个长字符串,在for循环的每次迭代结束时都添加换行符。

body=''
for item in parsed:
    body = body + str( item['resource']['caseNumber'], \
        item['resource']['subject'], \
        item['resource']['status'], \
        item['resource']['severity'] + '\n' )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM