<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Collins</title>
    <description>The latest articles on DEV Community by Collins (@0th).</description>
    <link>https://dev.to/0th</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F559325%2F043f52b4-f82d-43d3-86ea-380365f305d3.png</url>
      <title>DEV Community: Collins</title>
      <link>https://dev.to/0th</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/0th"/>
    <language>en</language>
    <item>
      <title>Read, Parse and Write CSV Files with Python.</title>
      <dc:creator>Collins</dc:creator>
      <pubDate>Fri, 17 Sep 2021 02:48:40 +0000</pubDate>
      <link>https://dev.to/0th/read-parse-and-write-csv-files-with-python-obj</link>
      <guid>https://dev.to/0th/read-parse-and-write-csv-files-with-python-obj</guid>
      <description>&lt;h2&gt;
  
  
  What are CSV files anyway?🤷‍♂️
&lt;/h2&gt;

&lt;p&gt;CSV files, the &lt;strong&gt;CSV&lt;/strong&gt; meaning &lt;strong&gt;Comma Separated Values&lt;/strong&gt;, are text files used to store data.&lt;br&gt;&lt;br&gt;
CSV files emulate tabular data, except that each field is separated by a &lt;em&gt;comma&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr073jq2ud4g54yzz3uu2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr073jq2ud4g54yzz3uu2.PNG" alt="csv_file"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;CSV files can be manipulated with Python in either of these ways:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;traditional read/write methods&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;csv&lt;/code&gt; module
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  What would be covered-
&lt;/h2&gt;

&lt;p&gt;In this article, the focus would be manipulating CSV files using &lt;em&gt;Python's&lt;/em&gt; &lt;code&gt;csv&lt;/code&gt; module, why it is preferred over the usual read/write method would become apparent in a bit.&lt;br&gt;&lt;br&gt;
This article covers the concepts and code (in Python 🐍) used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;reading CSV files&lt;/li&gt;
&lt;li&gt;accessing field names/headers&lt;/li&gt;
&lt;li&gt;writing CSV files&lt;/li&gt;
&lt;li&gt;leveraging dictionary readers and writers&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Reading CSV files
&lt;/h2&gt;

&lt;p&gt;Without wasting any more time, let's get straight to reading CSV files 🚀.&lt;br&gt;&lt;br&gt;
The CSV file used in this article can be found &lt;a href=""&gt;here&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# import csv module
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;

&lt;span class="c1"&gt;# open the csv file with a context manager
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;records.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

  &lt;span class="c1"&gt;# using the csv reader function
&lt;/span&gt;  &lt;span class="n"&gt;csv_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;# loop through the csv_reader iterable object
&lt;/span&gt;  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;csv_reader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# print each line in the reader object
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ['first_name', 'last_name', 'email']
    ['John', 'Doe', 'john-doe@bogusemail.com']
    ['Mary', 'Smith-Robinson', 'maryjacobs@bogusemail.com']
    ['Dave', 'Smith', 'davesmith@bogusemail.com']
    ['Jane', 'Stuart', 'janestuart@bogusemail.com']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The block of code above prints each line in the CSV file.&lt;br&gt;&lt;br&gt;
This was achieved first by importing the &lt;code&gt;csv&lt;/code&gt; module, then a &lt;em&gt;context manager&lt;/em&gt; is used to open the CSV file, the CSV file is read into a file object referenced &lt;code&gt;csv_file&lt;/code&gt;, using the &lt;code&gt;open()&lt;/code&gt; function.&lt;br&gt;&lt;br&gt;
Using the &lt;code&gt;csv&lt;/code&gt; module's &lt;code&gt;reader()&lt;/code&gt; function, each line in the CSV file is parsed into a &lt;strong&gt;reader&lt;/strong&gt; object, &lt;code&gt;csv_reader&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
The &lt;em&gt;reader&lt;/em&gt; object is &lt;em&gt;iterable&lt;/em&gt;, it returns &lt;em&gt;each line&lt;/em&gt; in the CSV file &lt;em&gt;as lists&lt;/em&gt; when subjected to iteration. &lt;br&gt;
A quick &lt;code&gt;for&lt;/code&gt; loop and a &lt;code&gt;print()&lt;/code&gt; function would return each line in the CSV file 😊.  &lt;/p&gt;

&lt;p&gt;It is important to note, that the &lt;em&gt;reader&lt;/em&gt; object iterable when iterated, returns each line of the CSV file in a &lt;code&gt;list&lt;/code&gt; object where each &lt;em&gt;comma-separated field&lt;/em&gt; is a &lt;em&gt;list item&lt;/em&gt;.&lt;br&gt;&lt;br&gt;
This could be helpful to determine what field values would be returned, by &lt;em&gt;indexing&lt;/em&gt; the &lt;em&gt;list&lt;/em&gt; that is returned for every line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# open file with a context manager
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;records.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# create reader object
&lt;/span&gt;  &lt;span class="n"&gt;csv_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;# loop through reader object csv_reader
&lt;/span&gt;  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;csv_reader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# print the field values under the field header email
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    email
    john-doe@bogusemail.com
    maryjacobs@bogusemail.com
    davesmith@bogusemail.com
    janestuart@bogusemail.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code block above prints the last &lt;em&gt;field value&lt;/em&gt; for every line in the CSV file.&lt;br&gt;&lt;br&gt;
First, the file is read as before, by a context manager, into a file object.&lt;br&gt;&lt;br&gt;
The &lt;em&gt;reader&lt;/em&gt; object is created next, it gets iterated through with a &lt;code&gt;for&lt;/code&gt; loop, and within the &lt;code&gt;print()&lt;/code&gt; function, is where the &lt;em&gt;indexing&lt;/em&gt; of each list item (line in CSV file) is done.  &lt;/p&gt;
&lt;h2&gt;
  
  
  Field names
&lt;/h2&gt;

&lt;p&gt;Just as most &lt;em&gt;tables&lt;/em&gt; have &lt;em&gt;headers&lt;/em&gt;, so do most CSV files also contain &lt;strong&gt;field names&lt;/strong&gt;, the &lt;em&gt;field names&lt;/em&gt; of a csv file can be obtained also if present, using the &lt;code&gt;reader&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# open file with a context manager
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;records.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# create reader object
&lt;/span&gt;  &lt;span class="n"&gt;csv_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;# iterate through the csv_reader once
&lt;/span&gt;  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Field names: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_reader&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Field names: ['first_name', 'last_name', 'email']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the concept of &lt;em&gt;iterators&lt;/em&gt; or &lt;em&gt;generators&lt;/em&gt; (which are iterators) is not strange, it shouldn't be new to know that when a loop is used to iterate through an &lt;em&gt;iterator&lt;/em&gt;, it's the &lt;code&gt;next()&lt;/code&gt; function that keeps getting called on the &lt;em&gt;iterator&lt;/em&gt; each time.  &lt;/p&gt;

&lt;p&gt;Here the &lt;code&gt;next()&lt;/code&gt; function is called once, which could be seen as looping or iterating through the &lt;em&gt;reader&lt;/em&gt; object once, this returns the first line in the CSV file, which would most of the time be the &lt;em&gt;field names&lt;/em&gt;.  &lt;/p&gt;

&lt;p&gt;It shouldn't be confusing that the &lt;em&gt;reader&lt;/em&gt; object was called an &lt;em&gt;iterable&lt;/em&gt; previously and an &lt;em&gt;iterator&lt;/em&gt; here, because all &lt;em&gt;iterators&lt;/em&gt; are actually &lt;em&gt;iterable&lt;/em&gt;. &lt;br&gt;
If there's any confusion about the difference between an &lt;em&gt;iterator&lt;/em&gt; and an &lt;em&gt;iterable&lt;/em&gt;, this &lt;a href="https://www.geeksforgeeks.org/python-difference-iterable-iterator/#:~:text=Iterable%20is%20an%20object%2C%20which%20one%20can%20iterate%20over.&amp;amp;text=Iterator%20is%20an%20object%2C%20which,every%20iterable%20is%20an%20iterator." rel="noopener noreferrer"&gt;article&lt;/a&gt; from geeksforgeeks should help.  &lt;/p&gt;
&lt;h2&gt;
  
  
  Writing to CSV files
&lt;/h2&gt;

&lt;p&gt;Just as it is possible to read CSV files in Python, it is also possible to write comma-separated values or fields into CSV files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# open the file to read or get comma separated values or data from
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;records.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# create reader object
&lt;/span&gt;  &lt;span class="n"&gt;csv_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;# open/create the file to write comma separated values to
&lt;/span&gt;  &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;new_records.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;new_csv_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# create writer object
&lt;/span&gt;    &lt;span class="n"&gt;csv_writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_csv_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;delimiter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# iterate through the comma separated values of the initially opened file through the reader object
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;csv_lines&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;csv_reader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# write these values to the new file
&lt;/span&gt;      &lt;span class="n"&gt;csv_writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writerow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_lines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The block of code above would successfully read or copy &lt;em&gt;comma-separated values&lt;/em&gt; from a CSV file (&lt;em&gt;records.csv&lt;/em&gt;) into another (&lt;em&gt;new_records.csv&lt;/em&gt;).  &lt;/p&gt;

&lt;p&gt;As repeated in previous code blocks, the CSV file is opened within a context manager, two CSV files were opened in the block of code above, the initial CSV file was opened to read comma-separated values out of it, into a &lt;code&gt;csv&lt;/code&gt; module's reader object.  &lt;/p&gt;

&lt;p&gt;The next CSV file is also opened within a context manager, although in this case it was opened so comma-separated values could be &lt;em&gt;written&lt;/em&gt; into it.  &lt;/p&gt;

&lt;p&gt;Next, a &lt;strong&gt;writer&lt;/strong&gt; object was created using the &lt;code&gt;csv&lt;/code&gt; module's &lt;code&gt;writer()&lt;/code&gt; function, which takes as argument the CSV file object.  &lt;/p&gt;

&lt;p&gt;The next section in the code block contains a loop that iterates through the &lt;em&gt;reader&lt;/em&gt; object, to return each line copied from the &lt;em&gt;first file&lt;/em&gt; (&lt;em&gt;records.csv&lt;/em&gt;) that was opened into the &lt;code&gt;csv_lines&lt;/code&gt; variable, this variable, is passed to the &lt;em&gt;writer&lt;/em&gt; object's method &lt;code&gt;writerow()&lt;/code&gt;, which writes these values into the &lt;em&gt;last opened file&lt;/em&gt; (&lt;em&gt;new_records.csv&lt;/em&gt;).&lt;br&gt;&lt;br&gt;
Within the &lt;code&gt;csv.writer()&lt;/code&gt; function in the previous code block, a second argument was included - &lt;code&gt;delimiter='-'&lt;/code&gt;, which would write the values from the previously opened file into the newly opened file, but each field value would be separated by a hyphen (-) instead of a comma (,).  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpx0c3kpcks11fxnew6ai.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpx0c3kpcks11fxnew6ai.PNG" alt="records"&gt;&lt;/a&gt;  &lt;/p&gt;
&lt;h4&gt;
  
  
  First CSV file (comma-separated values were copied/read from it).
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs9b8byw623vfeacbw9d3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs9b8byw623vfeacbw9d3.jpg" alt="new_records_LI"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;h4&gt;
  
  
  Second CSV file (comma-separated values were written to it).
&lt;/h4&gt;

&lt;p&gt;The second CSV file looks very hard to read and could be problematic if the field values contain the delimiting character (the hyphen, "-"), looking again at the second image, the &lt;em&gt;email field&lt;/em&gt; of the second entry (&lt;a href="mailto:john-doe@bogusemail.com"&gt;john-doe@bogusemail.com&lt;/a&gt;), and the &lt;em&gt;last_name&lt;/em&gt; field of the third entry (Smith-Robinson) have hyphens, which is the delimiting character.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CSV files values are not always separated by a comma, as seen in the second file above, the &lt;em&gt;delimiting&lt;/em&gt; character could be arbitrary, &lt;em&gt;commas&lt;/em&gt; are mostly used as a convention, and in some cases to improve readability.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;csv&lt;/code&gt; module's &lt;code&gt;writer()&lt;/code&gt; function, knew to place field values that contained the delimiting character in double quotes, as seen in the image above. This would have otherwise made the file hard to read or use in a program.  &lt;/p&gt;
&lt;h2&gt;
  
  
  Using Dictionary readers and writers
&lt;/h2&gt;

&lt;p&gt;Although using the &lt;code&gt;csv&lt;/code&gt; module's &lt;code&gt;reader()&lt;/code&gt; and &lt;code&gt;writer()&lt;/code&gt; functions seems like the standard way to handle CSV files, there's a better way to read from and write to CSV files, that improves code readability, and helps explicitly manipulate and parse comma-separated-values, the way to achieve this would be using the &lt;code&gt;csv&lt;/code&gt; module's &lt;code&gt;DictReader()&lt;/code&gt; and &lt;code&gt;DictWriter()&lt;/code&gt; functions for reading from and writing to CSV files respectively.  &lt;/p&gt;
&lt;h3&gt;
  
  
  Reading CSV files with the &lt;code&gt;DictReader()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The next code block would show how to read from a CSV file using the &lt;code&gt;csv&lt;/code&gt; module's &lt;code&gt;DictReader()&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# open the file to be read in a context manager
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;records.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

  &lt;span class="c1"&gt;# create a DictReader object using the DictReader function
&lt;/span&gt;  &lt;span class="n"&gt;csv_dict_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DictReader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;# iterate through DictReader object
&lt;/span&gt;  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;csv_dict_reader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# print each line in the CSV file as an OrderedDict object
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    OrderedDict([('first_name', 'John'), ('last_name', 'Doe'), ('email', 'john-doe@bogusemail.com')])
    OrderedDict([('first_name', 'Mary'), ('last_name', 'Smith-Robinson'), ('email', 'maryjacobs@bogusemail.com')])
    OrderedDict([('first_name', 'Dave'), ('last_name', 'Smith'), ('email', 'davesmith@bogusemail.com')])
    OrderedDict([('first_name', 'Jane'), ('last_name', 'Stuart'), ('email', 'janestuart@bogusemail.com')])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reading from a CSV file using the &lt;code&gt;DictReader()&lt;/code&gt; function is very similar to using the &lt;code&gt;reader()&lt;/code&gt; function as shown in the code block above.&lt;br&gt;&lt;br&gt;
The first and obvious difference is that the &lt;code&gt;DictReader()&lt;/code&gt; function is used in place of the &lt;code&gt;reader()&lt;/code&gt; function, thereby returning a &lt;code&gt;DictReader&lt;/code&gt; object, as opposed to the &lt;code&gt;reader&lt;/code&gt; object of the &lt;code&gt;reader()&lt;/code&gt; function.&lt;br&gt;&lt;br&gt;
The second difference would be iterating through the &lt;code&gt;DictReader&lt;/code&gt; object an &lt;code&gt;OrderedDict&lt;/code&gt; object is returned for each line in the CSV file, as opposed to a &lt;code&gt;list&lt;/code&gt; object from a &lt;code&gt;reader&lt;/code&gt; object.&lt;br&gt;&lt;br&gt;
Due to the &lt;code&gt;OrderedDict&lt;/code&gt; object returned for each line in the CSV file, it makes it easy to index field values, as it would be indexed by the field headers rather than ambiguous index numbers.&lt;br&gt;&lt;br&gt;
A use-case is illustrated in the code block below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# open CSV file in a context manager
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;records.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

  &lt;span class="c1"&gt;# create a DictReader object
&lt;/span&gt;  &lt;span class="n"&gt;csv_dict_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DictReader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;# iterate through DictReader object
&lt;/span&gt;  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;csv_dict_reader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# get field values for the email field only
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;email&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    john-doe@bogusemail.com
    maryjacobs@bogusemail.com
    davesmith@bogusemail.com
    janestuart@bogusemail.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code block prints only the field values under the &lt;em&gt;email&lt;/em&gt; header, by indexing with the field header - &lt;em&gt;'email'&lt;/em&gt;. This substantially improves code readability, due to how explicit it is.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Writing to CSV files using the &lt;code&gt;DictWriter()&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# open CSV file to read comma separated values from it
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;records.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# create DictReader object using the DictReader function
&lt;/span&gt;  &lt;span class="n"&gt;csv_dict_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DictReader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;# open new CSV file to write comma separated values into it
&lt;/span&gt;  &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;new_records.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;new_csv_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="c1"&gt;# create a list of the field names or headers of the field values that would be written to the file
&lt;/span&gt;    &lt;span class="n"&gt;field_names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;first_name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;last_name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;email&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="c1"&gt;# create a DictWriter object using the DictWriter() function.
&lt;/span&gt;    &lt;span class="c1"&gt;# assign the field_names list above to the fieldnames parameter of the function
&lt;/span&gt;    &lt;span class="c1"&gt;# pass a tab character as the delimiting character
&lt;/span&gt;    &lt;span class="n"&gt;csv_dict_writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DictWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_csv_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fieldnames&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;field_names&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;delimiter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# write the field header into the CSV file
&lt;/span&gt;    &lt;span class="n"&gt;csv_dict_writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeheader&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# iterate through the values read from the previous file
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;csv_dict_reader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# write the comma separated values to the new CSV file
&lt;/span&gt;      &lt;span class="n"&gt;csv_dict_writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writerow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Output file:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpzwjfdhszr5hmrw1865f.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpzwjfdhszr5hmrw1865f.PNG" alt="new_records_2"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The code block above would read comma-separated values from one CSV file, and write these values into another CSV file, separating each field value in the new CSV file by a tab character. &lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;DictWriter()&lt;/code&gt; function is very similar to using the &lt;code&gt;writer()&lt;/code&gt; function, significant differences to be noted would be explained.&lt;/p&gt;

&lt;p&gt;Firstly, after opening the second CSV file for writing (within the second context manager), a list referenced by variable name field_names was created, the list items are the field headers or field names of the comma-separated values that would be written into the new CSV file, this explains why it is passed as an argument to the &lt;code&gt;DictWriter()&lt;/code&gt; function, by assigning it to the &lt;code&gt;fieldnames=&lt;/code&gt; parameter.  &lt;/p&gt;

&lt;p&gt;Secondly, after calling the &lt;code&gt;DictWriter()&lt;/code&gt; function, the next line contains a method of the &lt;code&gt;DictWriter&lt;/code&gt; object created in the previous line, the &lt;code&gt;writeheader()&lt;/code&gt; method, this just makes sure that the field headers or field names are included when the comma-separated values are written, field headers are written to the top of the CSV file.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Hopefully, the article has been able to put into perspective how easy it is to handle CSV files in Python, thanks to the &lt;code&gt;csv&lt;/code&gt; module, and further usage of concepts and methods explained should be a walk in the park.  &lt;/p&gt;

&lt;p&gt;If the CSV files would be worked with in a data-science-focused setting, using the &lt;code&gt;csv&lt;/code&gt; module is not advisable, the &lt;code&gt;pandas&lt;/code&gt; library should come in handy in such situations, as it contains functions and objects that are better suited for such tasks.&lt;br&gt;&lt;br&gt;
Although the objects that are very much compatible with pandas functions may be difficult to handle in a traditional Python program.&lt;br&gt;&lt;br&gt;
It should also be clear at this point why the traditional &lt;code&gt;read()&lt;/code&gt;, &lt;code&gt;write()&lt;/code&gt; methods would not be feasible when handling CSV files.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Hopefully, you've learnt how to-
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;read data from CSV files&lt;/li&gt;
&lt;li&gt;write to CSV files&lt;/li&gt;
&lt;li&gt;use dictionary readers to intuitively manipulate CSV files.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Other resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/library/csv.html" rel="noopener noreferrer"&gt;Python documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=YYXdXT2l-Gg&amp;amp;list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU" rel="noopener noreferrer"&gt;Corey Schafer Series&lt;/a&gt; on youtube.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, connect with me on &lt;a href="https://twitter.com/__0th__" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and &lt;a href="https://linkedin.com/in/0th" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;, let's vibe ✌🏽.&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>codenewbie</category>
      <category>programming</category>
    </item>
    <item>
      <title>Flask Beginnings</title>
      <dc:creator>Collins</dc:creator>
      <pubDate>Tue, 02 Mar 2021 23:59:26 +0000</pubDate>
      <link>https://dev.to/0th/flask-beginnings-39lk</link>
      <guid>https://dev.to/0th/flask-beginnings-39lk</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FFYIDNjD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FFYIDNjD.png" alt="flask-image"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flask&lt;/strong&gt; is a micro web-framework, &lt;em&gt;micro&lt;/em&gt; in the sense that it doesn't come with out-of-the-box functionalities, desired functionalities are rather added by extensions. Flask also does not rely on pre-existing third-party libraries or frameworks, Flask's main dependencies were all authored by the creator of Flask himself.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why you should learn Flask?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is easier to learn if you're new to web development, but not Python.&lt;/li&gt;
&lt;li&gt;It has a large and supportive community
&lt;/li&gt;
&lt;li&gt;It is faster to develop once you're grounded in your knowledge of Python. You can use it to easily complete your next side project
&lt;/li&gt;
&lt;li&gt;It is in demand, being one of the popular web frameworks, it is used a lot in production.
&lt;/li&gt;
&lt;li&gt;It can be used to build fast websites that can handle traffic, flask is &lt;a href="https://www.pinterest.com/" rel="noopener noreferrer"&gt;Pinterest's&lt;/a&gt; core technology, with &lt;em&gt;322 million active users&lt;/em&gt;.
&lt;/li&gt;
&lt;li&gt;Finally, the documentation is a treasure, as it is very &lt;em&gt;comprehensive&lt;/em&gt; and &lt;em&gt;beginner-friendly&lt;/em&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Requirements for learning flask
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Knowledge of basic &lt;em&gt;Python&lt;/em&gt; concepts (generators, decorators, e.t.c)
&lt;/li&gt;
&lt;li&gt;A computer with &lt;strong&gt;python,&lt;/strong&gt; &lt;strong&gt;a web browser&lt;/strong&gt;, and a &lt;strong&gt;text/code editor&lt;/strong&gt; installed.
&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;&lt;em&gt;brain&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installing Flask
&lt;/h2&gt;

&lt;p&gt;If you're familiar with the Interactive Python console, you know about &lt;strong&gt;&lt;em&gt;pip&lt;/em&gt;&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://pypi.org/project/pip/" rel="noopener noreferrer"&gt;PIP&lt;/a&gt; (Preferred Installer Program) is the package installer for Python and it can install any package from &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPi&lt;/a&gt; - Python Package Index.  &lt;/p&gt;

&lt;p&gt;pip comes pre-installed with Python since version 2.7+ and 3.4+, so you must have python installed, flask is also a package in PyPI.&lt;/p&gt;

&lt;p&gt;To check if you have pip installed open your command-line interface, and run the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your output looks like below, it means you have it installed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip X.Y.Z ...&lt;span class="se"&gt;\s&lt;/span&gt;ite-packages&lt;span class="se"&gt;\p&lt;/span&gt;ip &lt;span class="o"&gt;(&lt;/span&gt;python X.Y&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Else if it looks in anyway like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="s1"&gt;'pip'&lt;/span&gt; is not recognized as an internal or external &lt;span class="nb"&gt;command&lt;/span&gt;,
operable program or batch file.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you should download and install pip before continuing.  &lt;/p&gt;

&lt;p&gt;Once that is settled, flask can now be installed using pip, enter into the command-line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;flask
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  First Flask Web Application.
&lt;/h2&gt;

&lt;p&gt;Once you're done with the flask installation, you're ready to write your first flask web application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FCLxU0RI.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FCLxU0RI.jpg" alt="code-image"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;h1&amp;gt; Hello world &amp;lt;/h1&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is all the code you need to spin your first flask web app, don't worry if you don't understand anything yet, I'll explain the different components as you read on.  &lt;/p&gt;

&lt;p&gt;The first line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you've used Python, you're familiar with import statements, here you're importing the &lt;strong&gt;&lt;em&gt;Flask&lt;/em&gt;&lt;/strong&gt; class from the flask module.&lt;/p&gt;

&lt;p&gt;The next line we've got:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;em&gt;app&lt;/em&gt;&lt;/strong&gt; object is an &lt;strong&gt;&lt;em&gt;instance&lt;/em&gt;&lt;/strong&gt; of the &lt;strong&gt;&lt;em&gt;Flask class&lt;/em&gt;&lt;/strong&gt;, it is created by passing the __name__ argument to the Flask constructor, the __name__ argument contains information about the python script you're currently writing your web application in.&lt;br&gt;&lt;br&gt;
Flask uses the __name__ argument to determine the file to run.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;strong&gt;&lt;em&gt;app&lt;/em&gt;&lt;/strong&gt; object above handles all requests made from the client to the server.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The next block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;h1&amp;gt; Hello world &amp;lt;/h1&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the webserver receives requests, it passes them to the Flask application instance, but the flask application instance does not know what to do for each URL request, that's where the &lt;strong&gt;&lt;em&gt;decorator&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;function&lt;/em&gt;&lt;/strong&gt; above comes in.  &lt;/p&gt;

&lt;p&gt;The decorator &lt;em&gt;@app.route("/")&lt;/em&gt; contains a &lt;strong&gt;&lt;em&gt;route&lt;/em&gt;&lt;/strong&gt; method that takes a string (actually &lt;strong&gt;&lt;em&gt;a URL&lt;/em&gt;&lt;/strong&gt;) as an argument, and the function defined below it is a &lt;strong&gt;&lt;em&gt;view function&lt;/em&gt;&lt;/strong&gt;, whatever is returned by the function is the &lt;strong&gt;&lt;em&gt;response&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
When a request comes in, the &lt;em&gt;route&lt;/em&gt; method needs to know what code to run for each request, therefore it maps the &lt;em&gt;URL&lt;/em&gt; to the function defined below the decorator - &lt;em&gt;view function&lt;/em&gt;, where all the processing occurs before a &lt;em&gt;response&lt;/em&gt; is rendered.  &lt;/p&gt;


&lt;h2&gt;
  
  
  Flask Development Web Server
&lt;/h2&gt;

&lt;p&gt;Now to view your response in the web browser, you'll have to start the Flask server, flask comes with a development web server not suitable for production (for production you'd have to put it behind a WSGI (Web Server Gateway Interface) server like &lt;strong&gt;&lt;em&gt;Gunicorn&lt;/em&gt;&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;To start the flask server, you'd first have to make the webserver know what file to run, to do that you'll set the &lt;strong&gt;&lt;em&gt;FLASK_APP&lt;/em&gt;&lt;/strong&gt; environment variable to our &lt;em&gt;app.py&lt;/em&gt; web app file.&lt;/p&gt;

&lt;p&gt;To do that run the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="nv"&gt;FLASK_APP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;For Unix users replace &lt;strong&gt;&lt;em&gt;set&lt;/em&gt;&lt;/strong&gt; with &lt;strong&gt;&lt;em&gt;export&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now you can run your app on the server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;flask run

 &lt;span class="k"&gt;*&lt;/span&gt; Serving Flask app &lt;span class="s2"&gt;"app.py"&lt;/span&gt;
 &lt;span class="k"&gt;*&lt;/span&gt; Environment: production
   WARNING: This is a development server. Do not use it &lt;span class="k"&gt;in &lt;/span&gt;a production deployment.
   Use a production WSGI server instead.
 &lt;span class="k"&gt;*&lt;/span&gt; Debug mode: off
 &lt;span class="k"&gt;*&lt;/span&gt; Running on http://127.0.0.1:5000/ &lt;span class="o"&gt;(&lt;/span&gt;Press CTRL+C to quit&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server is started, now copy the address &lt;strong&gt;&lt;em&gt;&lt;a href="http://127.0.0.1:5000/" rel="noopener noreferrer"&gt;http://127.0.0.1:5000/&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt; to your browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2Fnm39PdS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2Fnm39PdS.png" alt="flask-run"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;em&gt;response&lt;/em&gt; returned by the &lt;em&gt;view function&lt;/em&gt; can get more complex and the need to return a response based on the context of the request would rather make you return &lt;strong&gt;Templates&lt;/strong&gt; rather than just a string containing HTML elements (a bunch of these inside the python code would make the code unmaintainable).  &lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Flask resources:
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://flask.palletsprojects.com/" rel="noopener noreferrer"&gt;The Flask Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.miguelgrinberg.com/" rel="noopener noreferrer"&gt;Micheal Grinberg blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.amazon.com/Flask-Web-Development-Developing-Applications/dp/1491991739#:~:text=Flask%20Web%20Development%3A%20Developing%20Web,9781491991732%3A%20Amazon.com%3A%20Books" rel="noopener noreferrer"&gt;Flask Web Development: Developing Web Applications with Python book&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>flask</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
