Content-Length: 239485 | pFad | http://github.com/python/cpython/issues/129489

48 Indentation changes itself when copying the code from the Editor into Python 3.13.1 results in IndentationError · Issue #129489 · python/cpython · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
marslinks opened this issue Jan 30, 2025 · 4 comments

Comments

@marslinks
Copy link

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

@JeffersGlass
Copy link
Contributor

Hi, and thanks for asking your question! Can you format your code in between sets of three backticks like this:

```
print(“Hello, world”)
# a comment that doesn’t get huge
print(“neat!”)
```

It will make this much easier for others to read.

@StanFromIreland
Copy link
Contributor

StanFromIreland commented Jan 31, 2025

You need to save your code and run python. For more information the the tutorials section on running Python code.

If you have other such questions in the future please ask in the help section of the Python Discuss.

@encukou
Copy link
Member

encukou commented Jan 31, 2025

Please use “paste mode” (the F3 key) for pasting text to the console.

Making it work without F3 on Windows is work in progress, tracked here: #124096

@encukou encukou closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2025
@marslinks
Copy link
Author

marslinks commented Feb 7, 2025

F3 key not worked, however I used Visualstudio to edit the code then run from cmd the script and then fixed an error in the Visualstudio and saved and run again with/from cmd and worked. Here is the description for the solution I found out and used: #124096 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/issues/129489

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy