Sam Flynn
Sam Flynn

Reputation: 27

Looping and delimiting array in a post requests

I'm trying to create a email notifier using post requests that formats the body like this. The data comes from an array.

data = ([row1, tab1, tab2, tab3], [row2, tab1, tab2, tab3])

Expected:

Row 1    tab1    tab2    tab3
Row 2    tab1    tab2    tab3

I had success using '\n'.join([str(data[x]) for x in range(0,2)]) however it includes the braces and comma as shown below

body = '\n'.join([str(data[x]) for x in range(0,2)])
requests.post("https://mail.api", 
auth=("api", "key"), 
data={"from": "to <from>", 
"to": "To <to>", 
"subject": "Subject", 
"text": body})

Result:

[Row 1, tab1, tab2, tab3]
[Row 2, tab1, tab2, tab3]

Upvotes: 0

Views: 23

Answers (1)

Rakesh
Rakesh

Reputation: 82785

One way to do it is to convert the list object to a string object and replace all unwanted characters.

Example:

data = (["row1", "tab1", "tab2", "tab3"], ["row2", "tab1", "tab2", "tab3"])

stringValue = ''
for i in data:
    stringValue += str(i).replace("[", "").replace("]", '').replace("'", '').capitalize()+"\n"

print stringValue

Output:

Row1, tab1, tab2, tab3
Row2, tab1, tab2, tab3

Upvotes: 1

Related Questions