forked from calistus-igwilo/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.py
44 lines (33 loc) · 1.07 KB
/
save.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Generate all even numbers from 2 to 18 inclusive. Then write each number
on a separate line fo the file called num.txt
"""
numbers = []
for i in range(2, 20, 2):
numbers.append(i)
print(numbers)
with open('num.txt', 'w') as file:
for num in numbers:
file.write(str(num) + '\n')
"""
The follwoing dictionary is given:
stocks = {'PLW': ['Playway', 350], 'BBT': ['Boombit', 22]}
save to stocks.json using the json package. Set the indent to 4
"""
import json
stocks = {'PLW': ['Playway', 350], 'BBT': ['Boombit', 22]}
with open('stocks.json', 'w') as file:
json.dump(stocks, file, indent=4)
"""
The following list are given:
headers = ['user_id', 'amount']
users = [['001', '1400'], ['004', '1300'], ['007', '900']]
Save the above data to the users.csv file (file in csv format - comma-separated
values)as shown below
"""
headers = ['user_id', 'amount']
users = [['001', '1400'], ['004', '1300'], ['007', '900']]
with open('users.csv', 'w') as file:
file.write(','.join(headers) + '\n')
for user in users:
file.write(','.join(user) + '\n')