From d1be5725ec0858798f7e95fabf89bcf42587efc4 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 28 May 2025 19:05:31 +1000 Subject: [PATCH] updates --- .DS_Store | Bin 0 -> 6148 bytes dev1001/.DS_Store | Bin 0 -> 6148 bytes dev1001/4.4_file_handling/csv_files.py | 20 ++++++++++++++++++ dev1001/4.4_file_handling/grades.csv | 4 +++- dev1001/4.4_file_handling/json_files.py | 12 +++++++++++ dev1001/4.4_file_handling/mini_challenge_1.py | 12 +++++++---- dev1001/4.4_file_handling/mini_challenge_3.py | 7 ++++++ dev1001/4.4_file_handling/mini_challenge_4.py | 10 +++++++++ dev1001/4.4_file_handling/text_files.py | 16 ++++++++++++++ dev1001/4.4_file_handling/updated_config.json | 6 +++++- dev1001/4.4_file_handling/user_prefs.json | 11 ++++++++++ diary_entry.txt | 2 ++ my_file.txt | 9 ++++++++ 13 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 .DS_Store create mode 100644 dev1001/.DS_Store create mode 100644 dev1001/4.4_file_handling/csv_files.py create mode 100644 dev1001/4.4_file_handling/json_files.py create mode 100644 dev1001/4.4_file_handling/text_files.py create mode 100644 dev1001/4.4_file_handling/user_prefs.json create mode 100644 diary_entry.txt create mode 100644 my_file.txt diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5ebe505601837c8f0cf017b00af5709ff9213de2 GIT binary patch literal 6148 zcmeHKO>fgc5S>j!V=F@B08)>Z5Vxv0v_eoWCQT03N)_PxIk)(7Aq~%PPss z;m8p3Y>1SX&+;`DX1n*o?O<=OJKqjw_wR7Ldv`wfy&E@g zKR6nED2_}0$-FlytWjfE9iGEy1S=Yx#-p;(lzfwDWMUa(EwVNRGD}O_>n5R z;ung20pnN2+H`DqZ!tyz_QkC4D=eRm=#U;#pB@8uFh;ig9QAyKfvT7&APR^Amr}s( zhhFF}niyCRgcNj;k(QtojIZ37~Jpimc@~hE@YQ zMtxY+s`M_$R}nr|UR255{cCRYB^j+X3FXcn9m&%Q(31CQlR@5^#oAkQ28RAD9h>*= zQ-2(VY1U|bQKhZ&x$_mL;%qywdq>Hvm-*Q=?fR3K+5`h4EH+t_p4?dD?7o!{GMzjbG^s5(10 zZa;Y99Y>=$c@JG+3j3niMT0B&0Ot&up7bVhl*ES^y<%E1{5cv%ht~jWRspMkRbX8K zULRZ(#*V?IMp-&g$q@ipMzyfx(eKayn?bf^6|f5YR|=@|LGPf8 zl+4~*NRIbf7v&8K8;@IRln_*AI~Iqx;yQ{LuDR?0I|i2;(F3!81Vjc~Sq1*80zUv* CX7NG* literal 0 HcmV?d00001 diff --git a/dev1001/4.4_file_handling/csv_files.py b/dev1001/4.4_file_handling/csv_files.py new file mode 100644 index 0000000..c0c4989 --- /dev/null +++ b/dev1001/4.4_file_handling/csv_files.py @@ -0,0 +1,20 @@ +import csv + +with open('grades.csv', newline='') as f: + reader = csv.DictReader(f) + # csv_reader = csv.reader(f) + # header = next(csv_reader) + for row in reader: + # name, subject, grade = row + print(f"{row['Name']} got {row['Grade']} in {row['Subject']}") # row is a dictionary + + +new_data = [ + ["Name", "Subject", "Score"], + ["Asshole", "History", "88"], + ["Eve", "Art", "92"] +] + +with open('grades.csv', 'a', newline='') as f: + csv_writer = csv.writer(f) + csv_writer.writerows(new_data) diff --git a/dev1001/4.4_file_handling/grades.csv b/dev1001/4.4_file_handling/grades.csv index d65d817..52bdf0f 100644 --- a/dev1001/4.4_file_handling/grades.csv +++ b/dev1001/4.4_file_handling/grades.csv @@ -1,4 +1,6 @@ Name,Subject,Grade Alice,Math,90 Bob,Science,85 -Charlie,Math,95 \ No newline at end of file +Charlie,Math,95Name,Subject,Score +Asshole,History,88 +Eve,Art,92 diff --git a/dev1001/4.4_file_handling/json_files.py b/dev1001/4.4_file_handling/json_files.py new file mode 100644 index 0000000..dc924be --- /dev/null +++ b/dev1001/4.4_file_handling/json_files.py @@ -0,0 +1,12 @@ +import json + +with open('config.json') as f: + data = json.load(f) # parses json into python dict/list + print(f'Username: {data["username"]}') + print(f'Recent files: {data["recent_files"][0]}') + +# modifying the in-memory Python data structures +data['recent_files'].append('new_file.txt') + +with open('updated_config.json', 'w') as f: + json.dump(data, f, indent=4) \ No newline at end of file diff --git a/dev1001/4.4_file_handling/mini_challenge_1.py b/dev1001/4.4_file_handling/mini_challenge_1.py index eb946c0..6286685 100644 --- a/dev1001/4.4_file_handling/mini_challenge_1.py +++ b/dev1001/4.4_file_handling/mini_challenge_1.py @@ -1,9 +1,13 @@ diary_file = "diary_entry.txt" # 1. Write two lines about your day (use 'w' mode) -with open('''YOUR CODE HERE''') as f: - pass # Replace this with your code +with open(diary_file, 'w') as f: + f.write("Dear\n") + f.write("Diary\n") + # 2. Read and print content (use 'r' mode) -with '''YOUR CODE HERE''' as f: - pass +with open(diary_file, 'r') as f: + for line in f: + print(line.strip()) + # print(f.read()) diff --git a/dev1001/4.4_file_handling/mini_challenge_3.py b/dev1001/4.4_file_handling/mini_challenge_3.py index 6f31481..81f57db 100644 --- a/dev1001/4.4_file_handling/mini_challenge_3.py +++ b/dev1001/4.4_file_handling/mini_challenge_3.py @@ -1,3 +1,10 @@ # Task: Read new_grades.csv (that was just created) and print only the # names of students who scored above 90. +import csv + +with open('new_grades.csv', newline='') as f: + reader = csv.DictReader(f) + for row in reader: + if int(row['Score']) > 90: + print(row['Name']) diff --git a/dev1001/4.4_file_handling/mini_challenge_4.py b/dev1001/4.4_file_handling/mini_challenge_4.py index 00ccd2b..ab42b15 100644 --- a/dev1001/4.4_file_handling/mini_challenge_4.py +++ b/dev1001/4.4_file_handling/mini_challenge_4.py @@ -2,3 +2,13 @@ # key-value pair "font_size": 12. Write this modified data to a new # file user_prefs.json. +import json + +with open('updated_config.json') as f: + data = json.load(f) + +data['theme'] = 'light' +data['font_size'] = '12' + +with open('user_prefs.json', 'w') as f: + json.dump(data, f, indent=4) \ No newline at end of file diff --git a/dev1001/4.4_file_handling/text_files.py b/dev1001/4.4_file_handling/text_files.py new file mode 100644 index 0000000..626ef47 --- /dev/null +++ b/dev1001/4.4_file_handling/text_files.py @@ -0,0 +1,16 @@ +file = open('my_file.txt', 'a') # default is read +file.write("Hello\n") +file.close() + +# Read +# 'with' automatically closes the file when it ends +with open('my_file.txt') as f: + # files internally track their current position + # all_content = f.read(3) + # print(all_content) # reads 3 characters + first_line = f.readline() + print(first_line.strip()) + second_line = f.readline() + print(second_line) + + diff --git a/dev1001/4.4_file_handling/updated_config.json b/dev1001/4.4_file_handling/updated_config.json index e76f2af..e4d8f9b 100644 --- a/dev1001/4.4_file_handling/updated_config.json +++ b/dev1001/4.4_file_handling/updated_config.json @@ -2,5 +2,9 @@ "username": "admin", "theme": "dark", "notifications_enabled": true, - "recent_files": ["doc1.txt", "report.pdf", "new_file.txt"] + "recent_files": [ + "doc1.txt", + "report.pdf", + "new_file.txt" + ] } \ No newline at end of file diff --git a/dev1001/4.4_file_handling/user_prefs.json b/dev1001/4.4_file_handling/user_prefs.json new file mode 100644 index 0000000..e522c9a --- /dev/null +++ b/dev1001/4.4_file_handling/user_prefs.json @@ -0,0 +1,11 @@ +{ + "username": "admin", + "theme": "light", + "notifications_enabled": true, + "recent_files": [ + "doc1.txt", + "report.pdf", + "new_file.txt" + ], + "font_size": "12" +} \ No newline at end of file diff --git a/diary_entry.txt b/diary_entry.txt new file mode 100644 index 0000000..d0f5cd5 --- /dev/null +++ b/diary_entry.txt @@ -0,0 +1,2 @@ +Dear +Diary diff --git a/my_file.txt b/my_file.txt new file mode 100644 index 0000000..67e2f2b --- /dev/null +++ b/my_file.txt @@ -0,0 +1,9 @@ +HelloHelloHello +Hello +Hello +Hello +Hello +Hello +Hello +Hello +Hello