{"id":81,"date":"2026-03-01T17:17:48","date_gmt":"2026-03-01T12:17:48","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=81"},"modified":"2026-03-14T07:49:43","modified_gmt":"2026-03-14T02:49:43","slug":"working-with-csv-files","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/working-with-csv-files\/","title":{"rendered":"Working with CSV Files"},"content":{"rendered":"\n<p>A <strong>CSV (Comma-Separated Values)<\/strong> file is used to store tabular data such as spreadsheets and databases.<br>Each line represents a row, and values are separated by commas.<\/p>\n\n\n\n<p>Python provides a built-in <code>csv<\/code> module to work with CSV files easily.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>IMPORTING CSV MODULE<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">import csv<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>READING A CSV FILE<\/strong><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Example CSV File (data.csv)<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">Name,Age,City<br>Hira,25,Karachi<br>Ali,30,Lahore<br>Sara,22,Islamabad<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reading CSV File<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">import csvwith open(\"data.csv\", \"r\") as file:<br>    reader = csv.reader(file)<br>    for row in reader:<br>        print(row)<\/pre>\n\n\n\n<p>Each row is returned as a list.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>SKIPPING HEADER ROW<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">import csvwith open(\"data.csv\", \"r\") as file:<br>    reader = csv.reader(file)<br>    next(reader)   # Skip header<br>    for row in reader:<br>        print(row)<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>USING DICTREADER<\/strong><\/h1>\n\n\n\n<p><code>DictReader<\/code> reads CSV data as dictionaries.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import csvwith open(\"data.csv\", \"r\") as file:<br>    reader = csv.DictReader(file)<br>    for row in reader:<br>        print(row[\"Name\"], row[\"Age\"])<\/pre>\n\n\n\n<p>This makes it easier to access columns by name.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WRITING TO A CSV FILE<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">import csvwith open(\"new_data.csv\", \"w\", newline=\"\") as file:<br>    writer = csv.writer(file)<br>    writer.writerow([\"Name\", \"Age\", \"City\"])<br>    writer.writerow([\"Hira\", 25, \"Karachi\"])<br>    writer.writerow([\"Ali\", 30, \"Lahore\"])<\/pre>\n\n\n\n<p><code>newline=\"\"<\/code> prevents extra blank lines on some systems.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WRITING USING DICTWRITER<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">import csvdata = [<br>    {\"Name\": \"Hira\", \"Age\": 25, \"City\": \"Karachi\"},<br>    {\"Name\": \"Ali\", \"Age\": 30, \"City\": \"Lahore\"}<br>]with open(\"dict_data.csv\", \"w\", newline=\"\") as file:<br>    fieldnames = [\"Name\", \"Age\", \"City\"]<br>    writer = csv.DictWriter(file, fieldnames=fieldnames)    writer.writeheader()<br>    writer.writerows(data)<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>APPENDING TO A CSV FILE<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">import csvwith open(\"data.csv\", \"a\", newline=\"\") as file:<br>    writer = csv.writer(file)<br>    writer.writerow([\"Sara\", 22, \"Islamabad\"])<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>IMPORTANT POINTS<\/strong><\/h1>\n\n\n\n<p>\u2022 Always use <code>with<\/code> to handle files safely<br>\u2022 Use <code>newline=\"\"<\/code> when writing CSV files<br>\u2022 Use <code>DictReader<\/code> and <code>DictWriter<\/code> for better readability<br>\u2022 CSV files store data in plain text format<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHY CSV FILES ARE IMPORTANT<\/strong><\/h1>\n\n\n\n<p>\u2022 Common format for data exchange<br>\u2022 Used in Excel and databases<br>\u2022 Essential for data analysis<br>\u2022 Widely used in business and automation<\/p>\n\n\n\n<p>Understanding how to work with <strong>CSV files<\/strong> is essential for handling structured data in Python applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON INTERMEDIATE (PYI) > File Handling > Working with CSV Files<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773456623692\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":33,"template":"","class_list":["post-81","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Working with CSV Files - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python CSV file handling: read, write, and append data using csv.reader, DictReader, csv.writer, and DictWriter.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/working-with-csv-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Working with CSV Files - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python CSV file handling: read, write, and append data using csv.reader, DictReader, csv.writer, and DictWriter.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/working-with-csv-files\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-14T02:49:43+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/working-with-csv-files\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/working-with-csv-files\\\/\",\"name\":\"Working with CSV Files - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T12:17:48+00:00\",\"dateModified\":\"2026-03-14T02:49:43+00:00\",\"description\":\"Learn Python CSV file handling: read, write, and append data using csv.reader, DictReader, csv.writer, and DictWriter.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/working-with-csv-files\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/working-with-csv-files\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/working-with-csv-files\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > File Handling > Working with CSV Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Working with CSV Files - One Language. Endless Possibilities","description":"Learn Python CSV file handling: read, write, and append data using csv.reader, DictReader, csv.writer, and DictWriter.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/working-with-csv-files\/","og_locale":"en_US","og_type":"article","og_title":"Working with CSV Files - One Language. Endless Possibilities","og_description":"Learn Python CSV file handling: read, write, and append data using csv.reader, DictReader, csv.writer, and DictWriter.","og_url":"https:\/\/gigz.pk\/python\/lesson\/working-with-csv-files\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-14T02:49:43+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/working-with-csv-files\/","url":"https:\/\/gigz.pk\/python\/lesson\/working-with-csv-files\/","name":"Working with CSV Files - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T12:17:48+00:00","dateModified":"2026-03-14T02:49:43+00:00","description":"Learn Python CSV file handling: read, write, and append data using csv.reader, DictReader, csv.writer, and DictWriter.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/working-with-csv-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/working-with-csv-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/working-with-csv-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > File Handling > Working with CSV Files"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}