[LABI] tema04 csv and json examples added

This commit is contained in:
TiagoRG 2023-03-25 16:35:54 +00:00
parent 8d0e433173
commit 2b621e2400
7 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,12 @@
id,time,timestamp,value
1,15/03/2014 18:07:24,1394903244.0,2.3
1,15/03/2014 18:08:24,1394903304.0,1.8
1,15/03/2014 18:09:24,1394903364.0,1.2
1,15/03/2014 18:10:24,1394903424.0,1.6
1,15/03/2014 18:11:24,1394903484.0,2.1
1,15/03/2014 18:12:24,1394903544.0,2.5
1,15/03/2014 18:13:24,1394903604.0,2.9
1,15/03/2014 18:14:24,1394903664.0,3.3
1,15/03/2014 18:15:24,1394903724.0,3.0
1,15/03/2014 18:16:24,1394903784.0,2.8
1,15/03/2014 18:17:24,1394903844.0,2.4
1 id time timestamp value
2 1 15/03/2014 18:07:24 1394903244.0 2.3
3 1 15/03/2014 18:08:24 1394903304.0 1.8
4 1 15/03/2014 18:09:24 1394903364.0 1.2
5 1 15/03/2014 18:10:24 1394903424.0 1.6
6 1 15/03/2014 18:11:24 1394903484.0 2.1
7 1 15/03/2014 18:12:24 1394903544.0 2.5
8 1 15/03/2014 18:13:24 1394903604.0 2.9
9 1 15/03/2014 18:14:24 1394903664.0 3.3
10 1 15/03/2014 18:15:24 1394903724.0 3.0
11 1 15/03/2014 18:16:24 1394903784.0 2.8
12 1 15/03/2014 18:17:24 1394903844.0 2.4

View File

@ -0,0 +1,26 @@
entry,value
0,48
1,51
2,43
3,49
4,76
5,6
6,66
7,49
8,76
9,78
10,70
11,80
12,67
13,93
14,78
15,71
16,60
17,77
18,90
19,33
20,85
21,82
22,85
23,75
24,94
1 entry value
2 0 48
3 1 51
4 2 43
5 3 49
6 4 76
7 5 6
8 6 66
9 7 49
10 8 76
11 9 78
12 10 70
13 11 80
14 12 67
15 13 93
16 14 78
17 15 71
18 16 60
19 17 77
20 18 90
21 19 33
22 20 85
23 21 82
24 22 85
25 23 75
26 24 94

View File

@ -0,0 +1,12 @@
[
{
"time": 1394984189,
"name": "cpu",
"value": 12
},
{
"time": 1394984189,
"name": "cpu",
"value": 19
}
]

View File

@ -0,0 +1,16 @@
import csv
import random
def main(args=None):
with open("../datafiles/example2.csv", "w") as f:
writer = csv.DictWriter(f, delimiter=",", fieldnames=["entry", "value"])
writer.writeheader()
for i in range(25):
writer.writerow({"entry": i, "value": random.randint(0, 100)})
if __name__ == '__main__':
main()

View File

@ -0,0 +1,13 @@
import csv
import sys
def main(args=None):
with open(args[0], 'r') as f:
reader = csv.reader(f, delimiter=(',' if len(args) == 1 else args[1]))
for row in reader:
print(row)
if __name__ == '__main__':
main(sys.argv[1:])

View File

@ -0,0 +1,15 @@
import json
def main(args=None):
data = [
{"time": 1394984189, "name": "cpu", "value": 12},
{"time": 1394984189, "name": "cpu", "value": 19}
]
print(json.dumps(data, indent=4))
json.dump(data, open('../datafiles/example3.json', 'w'), indent=4)
if __name__ == '__main__':
main()

View File

@ -0,0 +1,10 @@
import json
def main(args=None):
data = json.load(open('../datafiles/example3.json', 'r'))
print(json.dumps(data, indent=4))
if __name__ == '__main__':
main()