{"id":106,"date":"2025-10-25T07:16:06","date_gmt":"2025-10-25T07:16:06","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=106"},"modified":"2025-12-17T07:49:51","modified_gmt":"2025-12-17T07:49:51","slug":"lesson-29-working-with-excel-and-csv-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=106","title":{"rendered":"Lesson 29: Working with Excel and CSV in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To learn how to <strong>read, write, and manipulate Excel and CSV files<\/strong> using Python, enabling data handling for real-life applications.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. Introduction<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CSV (<code>.csv<\/code>) \u2192 Comma Separated Values, simple text format<\/li>\n\n\n\n<li>Excel (<code>.xlsx<\/code>) \u2192 Microsoft Excel workbook<\/li>\n\n\n\n<li>Python libraries:\n<ul class=\"wp-block-list\">\n<li><strong><code>csv<\/code><\/strong> \u2192 built-in module for CSV<\/li>\n\n\n\n<li><strong><code>openpyxl<\/code><\/strong> \u2192 read\/write Excel (<code>.xlsx<\/code>)<\/li>\n\n\n\n<li><strong><code>pandas<\/code><\/strong> \u2192 powerful library for both Excel and CSV<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Working with CSV Files<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 1 \u2014 Reading a CSV File<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\n\nwith open(\"data.csv\", newline='') as csvfile:\n    reader = csv.reader(csvfile)\n    for row in reader:\n        print(row)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>CSV Example (<code>data.csv<\/code>):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name,Age,Grade\nAli,20,A\nSara,21,B\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'Name', 'Age', 'Grade']\n&#91;'Ali', '20', 'A']\n&#91;'Sara', '21', 'B']\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 2 \u2014 Writing to a CSV File<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\n\ndata = &#91;\n    &#91;\"Name\", \"Age\", \"Grade\"],\n    &#91;\"Ali\", 20, \"A\"],\n    &#91;\"Sara\", 21, \"B\"]\n]\n\nwith open(\"output.csv\", \"w\", newline='') as csvfile:\n    writer = csv.writer(csvfile)\n    writer.writerows(data)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Creates <code>output.csv<\/code> with the data.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 3 \u2014 Using <code>DictReader<\/code> and <code>DictWriter<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\n\n# Reading as dictionary\nwith open(\"data.csv\") as csvfile:\n    reader = csv.DictReader(csvfile)\n    for row in reader:\n        print(row&#91;\"Name\"], row&#91;\"Grade\"])\n\n# Writing as dictionary\nwith open(\"output_dict.csv\", \"w\", newline='') as csvfile:\n    fieldnames = &#91;\"Name\", \"Age\", \"Grade\"]\n    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n    writer.writeheader()\n    writer.writerow({\"Name\": \"Ali\", \"Age\": 20, \"Grade\": \"A\"})\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>3. Working with Excel Files using <code>openpyxl<\/code><\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 4 \u2014 Reading an Excel File<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from openpyxl import load_workbook\n\nwb = load_workbook(\"students.xlsx\")\nsheet = wb.active\n\nfor row in sheet.iter_rows(values_only=True):\n    print(row)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 5 \u2014 Writing to an Excel File<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from openpyxl import Workbook\n\nwb = Workbook()\nsheet = wb.active\nsheet.title = \"Students\"\n\ndata = &#91;\n    &#91;\"Name\", \"Age\", \"Grade\"],\n    &#91;\"Ali\", 20, \"A\"],\n    &#91;\"Sara\", 21, \"B\"]\n]\n\nfor row in data:\n    sheet.append(row)\n\nwb.save(\"students.xlsx\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 6 \u2014 Modifying Excel Data<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from openpyxl import load_workbook\n\nwb = load_workbook(\"students.xlsx\")\nsheet = wb.active\n\n# Change grade of Ali\nfor row in sheet.iter_rows(min_row=2):\n    if row&#91;0].value == \"Ali\":\n        row&#91;2].value = \"A+\"\n\nwb.save(\"students.xlsx\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>4. Using <code>pandas<\/code> for Excel &amp; CSV<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 7 \u2014 Reading CSV with Pandas<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\n\ndf = pd.read_csv(\"data.csv\")\nprint(df)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   Name  Age Grade\n0   Ali   20     A\n1  Sara   21     B\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 8 \u2014 Writing CSV with Pandas<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df.to_csv(\"output_pandas.csv\", index=False)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 9 \u2014 Reading Excel with Pandas<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df = pd.read_excel(\"students.xlsx\", sheet_name=\"Students\")\nprint(df)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example 10 \u2014 Writing Excel with Pandas<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df.to_excel(\"students_output.xlsx\", index=False, sheet_name=\"Students\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 <strong>5. Real-Life Example \u2014 Student Score Report<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\n\n# Read student marks\ndf = pd.read_csv(\"marks.csv\")\n\n# Calculate total and grade\ndf&#91;\"Total\"] = df&#91;\"Math\"] + df&#91;\"Science\"] + df&#91;\"English\"]\ndf&#91;\"Grade\"] = df&#91;\"Total\"].apply(lambda x: \"A\" if x &gt;= 270 else \"B\" if x &gt;= 240 else \"C\")\n\n# Save to Excel\ndf.to_excel(\"student_report.xlsx\", index=False)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Generates a ready-to-use Excel report with totals and grades.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To learn how to read, write, and manipulate Excel and CSV files using Python, enabling data handling for real-life applications. \ud83e\udde9 1. Introduction \u2699\ufe0f 2. Working with CSV Files Example 1 \u2014 Reading a CSV File CSV Example (data.csv): Output: Example 2 \u2014 Writing to a CSV File \u2705 Creates output.csv with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-106","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=106"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":107,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/106\/revisions\/107"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}