Skip to content

Commit 995b874

Browse files
committed
stepic ex
1 parent 966cc18 commit 995b874

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

stepic/csv_ops.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import csv
2+
3+
# print(csv.list_dialects())
4+
5+
# def reader_sample():
6+
# with open("Daily_Demand_Forecasting_Orders.csv") as df:
7+
# reader = csv.reader(df)
8+
# for row in reader:
9+
# print(row)
10+
11+
12+
# reader_sample()
13+
14+
# def sniffer():
15+
# with open("Daily_Demand_Forecasting_Orders.csv") as snif:
16+
# dialect = csv.Sniffer().sniff(snif.read(1024))
17+
# snif.seek(0)
18+
# has_header = csv.Sniffer().has_header(snif.read(1024))
19+
# snif.seek(0)
20+
# print("Headers: " + str(has_header))
21+
# print(dialect.delimiter)
22+
# print(dialect.escapechar)
23+
# print(dialect.quotechar)
24+
25+
26+
# sniffer()
27+
28+
29+
# def write_data():
30+
# with open("Daily_Demand_Forecasting_Orders.csv", mode="w") as csvfile:
31+
# csv_writer = csv.writer(csvfile)
32+
# csv_writer.writerow(["Week of the month (first week, second, third, fourth or fifth week",
33+
# "Day of the week (Monday to Friday)",
34+
# "Non-urgent order",
35+
# "Urgent order",
36+
# "Order type A",
37+
# "Order type B",
38+
# "Order type C",
39+
# "Fiscal sector orders",
40+
# "Orders from the traffic controller sector",
41+
# "Banking orders (1)",
42+
# "Banking orders (2)",
43+
# "Banking orders (3)",
44+
# "Target (Total orders)"])
45+
46+
# csv_writer.writerow(["1","4","316.307","223.270","61.543","175.586","302.448","0","65556","44914","188411","14793","539.577"])
47+
48+
# write_data()

stepic/json_parse.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import json
2+
3+
def main():
4+
5+
json_str = ''' {
6+
"make" : "Opel",
7+
"model" : "Mokka",
8+
"colours" : [
9+
"Black",
10+
"White",
11+
"Blue"
12+
],
13+
"price" : 20000.99
14+
}'''
15+
16+
try:
17+
data = json.loads(json_str)
18+
except:
19+
print('JSON Decode Error')
20+
21+
print("Car: " + data['make'])
22+
if (data['model'] == 'Mokka'):
23+
print('It\'s Mokka!')
24+
for colour in data['colours']:
25+
print('Colour: ' + colour)
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
if __name__ == "__main__":
37+
main()

stepic/json_ser.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import json
2+
3+
def main():
4+
5+
python_data = {
6+
"make" : "Opel",
7+
"model" : "Mokka",
8+
"colours" : [
9+
"Black",
10+
"White",
11+
"Blue"
12+
],
13+
"price" : 20000.99
14+
}
15+
16+
json_str = json.dumps(python_data, indent = 3)
17+
print(json_str)

stepic/temp_files.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import tempfile
3+
4+
print('gettempdir():', tempfile.gettempdir())
5+
print('gettempprefix():', tempfile.gettempprefix())
6+
7+
(tempfh, tempfp) = tempfile.mkstemp(".tmp", "test_temp", None, True)
8+
f = os.fdopen(tempfh, "w+t")
9+
f.write('Temp text data')
10+
f.seek(0)
11+
print(f.read())
12+
f.close()
13+
os.remove(tempfp)
14+
15+
with tempfile.TemporaryFile(mode='w+t') as tfp:
16+
tfp.write('Some more temp text data')
17+
tfp.seek(0)
18+
print(tfp.read())
19+
20+
21+
with tempfile.TemporaryDirectory() as tdp:
22+
path = os.path.join(tdp, "temp_text.txt")
23+
tfp = open(path, "w+t")
24+
tfp.write("The next temp text data")
25+
tfp.seek(0)
26+
print(tfp.read())
27+
tfp.close()

0 commit comments

Comments
 (0)