<?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>Forem: Nishant Patil</title>
    <description>The latest articles on Forem by Nishant Patil (@pnishant23).</description>
    <link>https://forem.com/pnishant23</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%2F937389%2Ffc6a7e46-2773-4c68-bedd-f5389705aae8.jpeg</url>
      <title>Forem: Nishant Patil</title>
      <link>https://forem.com/pnishant23</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pnishant23"/>
    <language>en</language>
    <item>
      <title>Integrate Google Sheets with Python: A Step-by-Step Guide</title>
      <dc:creator>Nishant Patil</dc:creator>
      <pubDate>Thu, 03 Apr 2025 10:25:29 +0000</pubDate>
      <link>https://forem.com/pnishant23/integrate-google-sheets-with-python-a-step-by-step-guide-2enh</link>
      <guid>https://forem.com/pnishant23/integrate-google-sheets-with-python-a-step-by-step-guide-2enh</guid>
      <description>&lt;p&gt;Want to learn how to connect your Google Sheets with Python to fetch and manipulate data? By using the &lt;code&gt;gspread&lt;/code&gt; library alongside &lt;code&gt;pandas&lt;/code&gt;, you can easily automate tasks, analyze data, and even save it in formats like JSON. Let’s walk through the process step-by-step—perfect for beginners and experienced users alike!&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Enable Google Sheets API &amp;amp; Get Credentials
&lt;/h2&gt;

&lt;p&gt;To integrate Google Sheets with Python, you first need to set up access via the Google Cloud Console:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Visit the &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;Google Cloud Console&lt;/a&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Create a new project (e.g., "Python-Google-Sheets").
&lt;img src="https://media2.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%2Fykwd659kjqafib91m1kw.png" alt="Image of creating a new project in google cloud console" width="800" height="420"&gt;
&lt;/li&gt;
&lt;li&gt;Go to &lt;strong&gt;"APIs &amp;amp; Services"&lt;/strong&gt; → &lt;strong&gt;"Library"&lt;/strong&gt;.
&lt;img src="https://media2.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%2Fai8etptfc7cly2yqt16l.png" alt="Google cloud console image" width="800" height="415"&gt;
&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Google Sheets API&lt;/strong&gt; and click &lt;strong&gt;"Enable"&lt;/strong&gt;.
&lt;img src="https://media2.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%2F98vfusaqw04ncfcontyp.png" alt="Enabling google sheets API" width="800" height="415"&gt;
&lt;img src="https://media2.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%2F8pq7khezgz3b3qyn1fqp.png" alt="Enabling google sheets API in google cloud console" width="800" height="417"&gt;
&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Google Drive API&lt;/strong&gt; and enable it too (required for file access).
&lt;img src="https://media2.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%2F1zfauxu8091kijfb85px.png" alt="Enabling google drive API" width="800" height="416"&gt;
&lt;img src="https://media2.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%2Fhkxfibzqfv0viux2oif4.png" alt="Enabling google drive API" width="800" height="416"&gt;
&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;"APIs &amp;amp; Services"&lt;/strong&gt; → &lt;strong&gt;"Credentials"&lt;/strong&gt;.
&lt;img src="https://media2.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%2F4x0ozr9ofl5w4zmo9skk.png" alt="Google cloud console image" width="800" height="418"&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;"Create Credentials"&lt;/strong&gt; and choose &lt;strong&gt;"Service Account"&lt;/strong&gt;.
&lt;img src="https://media2.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%2F8cv2carnmds99nchgjqs.png" alt="Google cloud console image" width="800" height="416"&gt;
&lt;/li&gt;
&lt;li&gt;After creating the service account:

&lt;ul&gt;
&lt;li&gt;Go to the &lt;strong&gt;"Keys"&lt;/strong&gt; tab.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;"Add Key"&lt;/strong&gt; → &lt;strong&gt;"JSON"&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A JSON key file (e.g., your-credentials.json) will download to your computer.
&lt;img src="https://media2.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%2Fy8k7dy5dp2ddmol4bujc.png" alt="Google cloud console image" width="800" height="414"&gt;
&lt;img src="https://media2.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%2Fw79uzt0lwdnvwje46ivd.png" alt="Google cloud console image" width="800" height="415"&gt;
&lt;img src="https://media2.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%2F97a9hslxae527j9oyg8q.png" alt="Google cloud console image" width="800" height="418"&gt;
&lt;strong&gt;Note&lt;/strong&gt;: This JSON file contains your service account credentials. Keep it secure and don’t share it publicly!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 2: Install Required Libraries
&lt;/h2&gt;

&lt;p&gt;You’ll need two Python libraries: &lt;code&gt;gspread&lt;/code&gt;for interacting with Google Sheets and &lt;code&gt;pandas&lt;/code&gt;for handling data. Install them using pip:&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;gspread pandas
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure you run this command in your terminal or command prompt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Load Google Sheets Data in Python
&lt;/h2&gt;

&lt;p&gt;Now, let’s write some Python code to fetch data from your Google Sheet:&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;import&lt;/span&gt; &lt;span class="n"&gt;gspread&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="c1"&gt;# Load credentials from the JSON file*
&lt;/span&gt;&lt;span class="n"&gt;gc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gspread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;service_account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;your-credentials.json&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="c1"&gt;# Replace with your JSON file path# Open the Google Sheet by its name*
&lt;/span&gt;&lt;span class="n"&gt;sh&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gc&lt;/span&gt;&lt;span class="p"&gt;.&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;Your Google Sheet Name&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="c1"&gt;# Replace with your sheet’s exact name# Select a worksheet (e.g., "Sheet1")*
&lt;/span&gt;&lt;span class="n"&gt;worksheet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sh&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;worksheet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sheet1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="c1"&gt;# Replace with your worksheet name# Fetch all data from the worksheet*
&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;worksheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_all_records&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="c1"&gt;# Convert the data into a pandas DataFrame*
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="c1"&gt;# Display the DataFrame*
&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;df&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What’s Happening Here?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;gspread.service_account()&lt;/code&gt; authenticates using your JSON credentials.&lt;/li&gt;
&lt;li&gt;gc.open() accesses your Google Sheet by its title (found in the browser tab).&lt;/li&gt;
&lt;li&gt;worksheet.get_all_records() retrieves all rows as a list of dictionaries, assuming the first row is headers.&lt;/li&gt;
&lt;li&gt;pd.DataFrame() turns the data into a table-like structure for easy manipulation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 4: Share Google Sheet with Service Account
&lt;/h2&gt;

&lt;p&gt;Your Python script won’t work yet—your service account needs permission to access the sheet:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your Google Sheet in a browser.&lt;/li&gt;
&lt;li&gt;Click the &lt;strong&gt;"Share"&lt;/strong&gt; button (top-right corner).&lt;/li&gt;
&lt;li&gt;Find the service account email in your JSON file (it looks like &lt;a href="mailto:your-service-account@project-id.iam.gserviceaccount.com"&gt;your-service-account@project-id.iam.gserviceaccount.com&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Add this email to the share list and grant &lt;strong&gt;Editor&lt;/strong&gt; access.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fy3ho7a95d7uvzntu8hgn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fy3ho7a95d7uvzntu8hgn.png" alt="google sheets image" width="800" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt; The service account acts like a user and needs explicit permission to read or edit the sheet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Useful Methods
&lt;/h2&gt;

&lt;p&gt;Once connected, &lt;code&gt;gspread&lt;/code&gt;offers handy methods to interact with your sheet. Here are a few examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  Get Data from a Specific Cell
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;value &lt;span class="o"&gt;=&lt;/span&gt; worksheet.acell&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"B2"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;.value  &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Fetch the value in cell B2*&lt;/span&gt;
print&lt;span class="o"&gt;(&lt;/span&gt;value&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Get Data from a Specific Row or Column
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;row_values &lt;span class="o"&gt;=&lt;/span&gt; worksheet.row_values&lt;span class="o"&gt;(&lt;/span&gt;2&lt;span class="o"&gt;)&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Get all values in row 2*&lt;/span&gt;
col_values &lt;span class="o"&gt;=&lt;/span&gt; worksheet.col_values&lt;span class="o"&gt;(&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Get all values in column 1*&lt;/span&gt;
print&lt;span class="o"&gt;(&lt;/span&gt;row_values, col_values&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Append a New Row
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;worksheet.append_row&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;"John Doe"&lt;/span&gt;, 25, &lt;span class="s2"&gt;"Engineer"&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Add a new row to the sheet*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These methods are great for tasks like updating records, extracting specific data, or automating reports.&lt;/p&gt;

&lt;h2&gt;
  
  
  Save Data to a JSON File
&lt;/h2&gt;

&lt;p&gt;Want to save your Google Sheets data as a JSON file? Add this code after fetching the data:&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;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="c1"&gt;# Define the file path*
&lt;/span&gt;&lt;span class="n"&gt;file_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;./data.json&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;dir_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="c1"&gt;# Create the directory if it doesn’t exist*
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dir_name&lt;/span&gt;&lt;span class="p"&gt;):&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="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Directory &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;dir_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; does not exist.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&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="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Creating the directory: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;dir_name&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;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makedirs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dir_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&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="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Directory &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;dir_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; already exists.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="c1"&gt;# Check if the file exists and create or modify it*
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="p"&gt;):&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="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;File &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;file_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; does not exist.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&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="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Creating a new file: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;file_name&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;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&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="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;File already exists, modifying the file.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="c1"&gt;# Save the data to JSON*
&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="n"&gt;file_name&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;json_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What’s Happening?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script checks if the directory and file exist, creating them if needed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;json.dump()&lt;/code&gt; writes the data to &lt;em&gt;data.json&lt;/em&gt; with nice formatting (indent=4).&lt;/li&gt;
&lt;li&gt;The result is a JSON file you can use elsewhere, like in a web app or for backups.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;You’ve now learned how to integrate Google Sheets with Python! From setting up the API and fetching data to saving it as JSON, this setup opens up endless possibilities for automation and analysis. Try it out with your own Google Sheet—what will you build with this power?&lt;/p&gt;

&lt;p&gt;Feel free to experiment with the code and share your ideas!&lt;/p&gt;

</description>
      <category>python</category>
      <category>googlesheets</category>
      <category>programming</category>
      <category>automation</category>
    </item>
    <item>
      <title>A Beginner's Guide to Searching for Elements in Arrays with JavaScript</title>
      <dc:creator>Nishant Patil</dc:creator>
      <pubDate>Fri, 14 Apr 2023 19:03:07 +0000</pubDate>
      <link>https://forem.com/pnishant23/a-beginners-guide-to-searching-for-elements-in-arrays-with-javascript-535b</link>
      <guid>https://forem.com/pnishant23/a-beginners-guide-to-searching-for-elements-in-arrays-with-javascript-535b</guid>
      <description>&lt;p&gt;Lets talk about searching an element in an array. There are various algorithm used for this purpose, but the one which I am going to discuss is &lt;strong&gt;Linear Search&lt;/strong&gt;. Linear Search is simple and easy to understand. There is also a default javascript method for for linear search, will will talk about it later.&lt;/p&gt;

&lt;p&gt;Linear Search is simple it uses a &lt;strong&gt;for loop&lt;/strong&gt;, which loops through the given array and returns its index. If the index is a value then we know that the value exist , if its undefined then the value is absent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  let data =[20,40,60,5,10,70,80,99];
        let item=70;
        let index=undefined;

        for(i=0;i&amp;lt;=data.length-1;i++){
             if(data[i]===item)
             {
                index=i;
                 break;
             }
         }
         console.log(index);

         // default method to search an element in array
         console.warn(data.indexOf(item))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can copy paste the above code and test it. &lt;br&gt;
The defualt funciton used of searching an element in array is &lt;code&gt;indexOf()&lt;/code&gt; method. By passing the element that you have in array inside &lt;code&gt;indexOf()&lt;/code&gt; method it will return its index. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginnerfriendly</category>
      <category>datastructures</category>
      <category>linearsearchinjavascript</category>
    </item>
    <item>
      <title>Delete an element in array</title>
      <dc:creator>Nishant Patil</dc:creator>
      <pubDate>Thu, 13 Apr 2023 17:44:20 +0000</pubDate>
      <link>https://forem.com/pnishant23/delete-an-element-in-array-1pk4</link>
      <guid>https://forem.com/pnishant23/delete-an-element-in-array-1pk4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Theory :&lt;/strong&gt;&lt;br&gt;
Consider a Array declared as&lt;br&gt;
&lt;code&gt;let arr=[1,2,3,4,5]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Suppose we want to delete the element at index 2, that means we have to replace the element at index 3 to position at index 2 and same for all other elements .&lt;/p&gt;

&lt;p&gt;Suppose we want to delete the element at index n. We will do this by replacing the element at index n+1 to n, replacing the element at index n+2 to n+1, similarly for all other elements. And once the element is deleted and all other elements are at new place, then we will decrease the length the of array by 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let data  =[30,20,45,76,20,80];
let position =3;
for(let i=position; i&amp;lt;data.length-1;i++){
    data[i]=data[i+1];
}
data.length= data.length-1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>arraymanipulation</category>
      <category>webdev</category>
      <category>dsa</category>
      <category>javascript</category>
    </item>
    <item>
      <title>"How Does JavaScript's splice Function Really Work?"</title>
      <dc:creator>Nishant Patil</dc:creator>
      <pubDate>Thu, 13 Apr 2023 06:28:54 +0000</pubDate>
      <link>https://forem.com/pnishant23/how-does-javascripts-slice-function-really-work-56pk</link>
      <guid>https://forem.com/pnishant23/how-does-javascripts-slice-function-really-work-56pk</guid>
      <description>&lt;p&gt;JavaScript splice method is used to add a new element in an array without deleting any element in it. Lets learn how splice method works behind the scene.&lt;/p&gt;

&lt;p&gt;Theory :&lt;br&gt;
Consider a Array declared as&lt;br&gt;
 &lt;code&gt;let arr=[1,2,3,4,5]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Suppose we want to add new element at index 2, that means we have change the position of element at index 2 by replacing it somewhere else and add a new value there. &lt;/p&gt;

&lt;p&gt;We will do so by replacing the position of last element to it's n+1 position (if there are n elements consider) which means we will replace the number 5 to a new position at index 5 (remember indexing starts from 0). Similarly we will shift the second last element to the position of last element and this process will continue until the element at position that we have to place the new number is replaced. This will create a empty space at index 2 where we will will insert our element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let data =[60,30,10,67,40];
let newEl= 70;
let position=2;
for(let i=data.length-1;i&amp;gt;=0;i--){
      if(i&amp;gt;=position){
          data[i+1]=data[i];
          if(i==position){
              data[i]=newEl;
          }
      }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Practical explanation : &lt;br&gt;
As seen from the code we have declared some variables, array, new element and position where we want to enter new element. &lt;/p&gt;

&lt;p&gt;Next a for loop is created which will starts from the last element. &lt;br&gt;
First the loop will changed the position of element as discussed above. &lt;br&gt;
If the the position where we want to insert element will reach, the loop does adds a new element to that position. &lt;/p&gt;

&lt;p&gt;Thats how the splice() method in JavaScript works behind the scenes. The code syntax may be different but logical and approach is same. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
