Class Notes
Date: 10/05/2025
# Writing to [Link]
with open('[Link]', 'w') as f:
[Link]("Day 1: I started learning Python!\n")
[Link]("Day 2: I learned how to write files.")
00:28:35 Jaid Jashim:# Reading from [Link]
with open('[Link]', 'r') as f:
print([Link]())
_________________________________________________
# Writing to [Link]
with open('[Link]', 'w') as f:
[Link]("Day 1: I started learning Python!\n")
[Link]("Day 2: I learned how to write files.")
00:28:35 Jaid Jashim:# Reading from [Link]
with open('[Link]', 'r') as f:
print([Link]())
_________________________________________________
import csv
custom_dat = [
["user_name", "user_role", "user_address", "user_contact_number"],
["Person_A", "student", "Dhaka", 1711111111 ],
["Person_B", "Teacher", "Dhaka", 1622222222 ],
["Person_C", "Instructor", "Sylhet", 18333333333 ],
["Person_D", "Class Captain", "Chittagong", 1944444444444 ]
with open("customer_file.csv", mode="w", newline="") as file:
writer = [Link](file)
[Link](custom_dat)
_________________________________________________
import csv
student_result = [
["stuend_name","student_result", "student_result_grade"],
["studentA", 60, "B"],
["studentB", 65, "B"],
["studentC", 85, "A"],
["studentD", 70, "A"],
["studentE", 50, "C"],
["studentF", 40, "C"],
["studentG", 55, "C"],
["studentH", 50, "C"],
["studentI", 10, "F"],
with open("students_result_file.csv", mode="w", newline="") as file:
writer = [Link](file)
[Link](student_result)
_________________________________________________
import pandas as pd
data = pd.read_csv("students_result_file.csv")
[Link]()
print(data[data['student_result_grade'] == 'C'])
print(data[data['student_result'] <= 60])
_________________________________________________
Common Options for newline=
OptionᅠDescription
newline=NoneᅠDefault. Universal newline mode: all \n, \r\n, and \r are translated to \n
on reading and back to system default on writing.
newline=''ᅠNo translation: newline characters are written and read as-is. Often used with
csv to prevent extra blank lines on Windows.
newline='\n'ᅠUse only line feed. Suitable for Unix/Linux-style newlines.
newline='\r\n'ᅠUse carriage return + line feed. Standard for Windows text files.
newline='\r'ᅠUse carriage return only (classic Mac-style, rarely used today).
_________________________________________________
import json
data = {"b": 22, "a": 100, "d": 46, "c": 89, "dfsdf": 78}
print([Link](data, indent=2, sort_keys=True))
#{
# "a": 1,
# "b": 2
#}
01:55:22 Jaid Jashim:import json
# Step 1: Load JSON data from file
with open("[Link]", "r", encoding="utf-8") as f:
data = [Link](f)
# Step 2: Access specific values
print("Name:", data["name"])
print("Favorite food:", data["favorite_food"])
# print("Microchipped:", [Link]("microchipped"))
# Step 3: Update some values
data["age"] += 2 # birthday!
data["favorite_food"] = "Burger" # new favorite
data["last_vet_visit"] = "2025-07-20" # updated visit
# Step 4: Add new entries
data["disliked_fool"] = "vegetables"
data["daily_study_hours"] = 4
# Step 5: Remove a key (if exists)
[Link]("likes_water", None) # safely remove without error if not found
# Step 6: Loop through all key-value pairs
🐶 Updated Dog Profile:")
print("\n
for key, value in [Link]():
print(f"{key}: {value}")
# Optional: Save the updated data back to file
with open("output_updated.json", "w", encoding="utf-8") as f:
[Link](data, f, indent=2)
_________________________________________________
Use CaseᅠCSVᅠJSON
Tabular data (rows/columns)ᅠ✅ Yesᅠ❌ Not Ideal
Nested or structured dataᅠ❌ Noᅠ✅ Yes
API communicationᅠ❌ Rareᅠ✅ Standard
Editable in Excelᅠ✅ Easyᅠ❌ Hard
For configs/settingsᅠ❌ Not usedᅠ✅ Common
Small databasesᅠ✅ Simplerᅠ✅ Flexible