From efe3159d639cd28503c3d2e9e9de7e6544fbd591 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sat, 25 Mar 2023 16:35:54 +0000 Subject: [PATCH] [LABI] tema04 csv and json examples added --- .../labi/tema04/datafiles/example1.csv | 12 +++++++++ .../labi/tema04/datafiles/example2.csv | 26 +++++++++++++++++++ .../labi/tema04/datafiles/example3.json | 12 +++++++++ .../labi/tema04/src/csv_randomizer.py | 16 ++++++++++++ 1ano/2semestre/labi/tema04/src/csv_reader.py | 13 ++++++++++ 1ano/2semestre/labi/tema04/src/json_dumper.py | 15 +++++++++++ 1ano/2semestre/labi/tema04/src/json_reader.py | 10 +++++++ 7 files changed, 104 insertions(+) create mode 100644 1ano/2semestre/labi/tema04/datafiles/example1.csv create mode 100644 1ano/2semestre/labi/tema04/datafiles/example2.csv create mode 100644 1ano/2semestre/labi/tema04/datafiles/example3.json create mode 100644 1ano/2semestre/labi/tema04/src/csv_randomizer.py create mode 100644 1ano/2semestre/labi/tema04/src/csv_reader.py create mode 100644 1ano/2semestre/labi/tema04/src/json_dumper.py create mode 100644 1ano/2semestre/labi/tema04/src/json_reader.py diff --git a/1ano/2semestre/labi/tema04/datafiles/example1.csv b/1ano/2semestre/labi/tema04/datafiles/example1.csv new file mode 100644 index 0000000..98e4377 --- /dev/null +++ b/1ano/2semestre/labi/tema04/datafiles/example1.csv @@ -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 \ No newline at end of file diff --git a/1ano/2semestre/labi/tema04/datafiles/example2.csv b/1ano/2semestre/labi/tema04/datafiles/example2.csv new file mode 100644 index 0000000..6fd2a40 --- /dev/null +++ b/1ano/2semestre/labi/tema04/datafiles/example2.csv @@ -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 diff --git a/1ano/2semestre/labi/tema04/datafiles/example3.json b/1ano/2semestre/labi/tema04/datafiles/example3.json new file mode 100644 index 0000000..7f0d41b --- /dev/null +++ b/1ano/2semestre/labi/tema04/datafiles/example3.json @@ -0,0 +1,12 @@ +[ + { + "time": 1394984189, + "name": "cpu", + "value": 12 + }, + { + "time": 1394984189, + "name": "cpu", + "value": 19 + } +] \ No newline at end of file diff --git a/1ano/2semestre/labi/tema04/src/csv_randomizer.py b/1ano/2semestre/labi/tema04/src/csv_randomizer.py new file mode 100644 index 0000000..32c92e9 --- /dev/null +++ b/1ano/2semestre/labi/tema04/src/csv_randomizer.py @@ -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() diff --git a/1ano/2semestre/labi/tema04/src/csv_reader.py b/1ano/2semestre/labi/tema04/src/csv_reader.py new file mode 100644 index 0000000..cf29dc1 --- /dev/null +++ b/1ano/2semestre/labi/tema04/src/csv_reader.py @@ -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:]) diff --git a/1ano/2semestre/labi/tema04/src/json_dumper.py b/1ano/2semestre/labi/tema04/src/json_dumper.py new file mode 100644 index 0000000..f2a5371 --- /dev/null +++ b/1ano/2semestre/labi/tema04/src/json_dumper.py @@ -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() diff --git a/1ano/2semestre/labi/tema04/src/json_reader.py b/1ano/2semestre/labi/tema04/src/json_reader.py new file mode 100644 index 0000000..0ab744d --- /dev/null +++ b/1ano/2semestre/labi/tema04/src/json_reader.py @@ -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()