Skip to content

Commit b25e1d7

Browse files
Export to CSV
1 parent 2060c79 commit b25e1d7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

0x15-api/1-export_to_CSV.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python3
2+
"""import api from a url"""
3+
import requests
4+
import sys
5+
6+
if __name__ == "__main__":
7+
try:
8+
id = int(sys.argv[1])
9+
url = "https://jsonplaceholder.typicode.com"
10+
res = requests.get('{}/users/{}'.format(url, id)).json()
11+
res_todo = requests.get("{}/todos".format(url),
12+
params={"userId": id}).json()
13+
name = res.get("username")
14+
with open('{}.csv'.format(id), 'w') as apidata:
15+
for todo in res_todo:
16+
apidata.write(
17+
'"{}","{}","{}","{}"\n'.format(id, name,
18+
todo.get('completed'),
19+
todo.get('title'))
20+
)
21+
except ValueError:
22+
pass

0 commit comments

Comments
 (0)