Skip to content

Indentation changes itself when copying the code from the Editor into Python 3.13.1 results in IndentationError #129489

@marslinks

Description

@marslinks

Hi,

I am a kind of beginner with Python and the following issue happened:

I have a code to extract information from a search engine and when I try to copy my code into Python 3.13.1 it destroy the intendation and I get the IndentationError.

import pandas as pd
import requests
import os

Load the input file

input_file = 'companies.csv'
df = pd.read_csv(input_file)

Your SerpAPI key

SERPAPI_KEY = 'PUT YOUR SERPAPI_KEY HERE'
base_url = 'https://serpapi.com/search'

Create a list to hold results

results = []

Loop through each company name and fetch website

for company in df['Company Name']:
params = {
'api_key': SERPAPI_KEY,
'engine': 'google',
'q': company,
'hl': 'en',
'gl': 'us'
}

response = requests.get(base_url, params=params)
data = response.json()

# Get the first organic result link

if 'organic_results' in data and len(data['organic_results']) > 0:
website = data['organic_results'][0].get('link')
results.append({'Company Name': company, 'Website': website})
else:
results.append({'Company Name': company, 'Website': None})

Create a DataFrame and save to CSV

results_df = pd.DataFrame(results)
results_df.to_csv('company_websites.csv', index=False)

And when I copy it into Python somehow the intendation will be destroyed and I got the IndentationError, because the
else:
results.append({'Company Name': company, 'Website': None})**
2 lines have more blank space then it should have to have.

So the code will look like this:
Python 3.13.1 (tags/v3.13.1:0671451, Dec 3 2024, 19:06:28) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import pandas as pd
import requests
import os

Load the input file

input_file = 'companies.csv'
df = pd.read_csv(input_file)

Your SerpAPI key

SERPAPI_KEY = 'PUT YOUR SERPAPI_KEY HERE'
base_url = 'https://serpapi.com/search'

Create a list to hold results

results = []

Loop through each company name and fetch website

for company in df['Company Name']:
... params = {
... 'api_key': SERPAPI_KEY,
... 'engine': 'google',
... 'q': company,
... 'hl': 'en',
... 'gl': 'us'
... }
...
response = requests.get(base_url, params=params)
data = response.json()

# Get the first organic result link

if 'organic_results' in data and len(data['organic_results']) > 0:
... website = data['organic_results'][0].get('link')
... results.append({'Company Name': company, 'Website': website})
... else:
... results.append({'Company Name': company, 'Website': None})
...
File "", line 3
results.append({'Company Name': company, 'Website': website})
IndentationError: unexpected indent

Create a DataFrame and save to CSV

results_df = pd.DataFrame(results)
results_df.to_csv('company_websites.csv', index=False)

I tried to write the code with Notepad, Notepad++ and VisualBasic and it happened everytime I copied the code into Python 3.13.
Next to it I have been trying to write the code directly into Python 3.13. and it started to run without letting me to finish the code.

Op. system: Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions