Python Arena Blog
Python tutorials
Python basics
2 articlesPython Console Window Closes Immediately: How to Fix
The console window belongs to the process: the script ends, the window disappears along with the traceback. Why that happens, how to run the file from cmd or PowerShell, what the py launcher is for, and how to redirect output and errors to a file. Plus the case where nothing failed at all.
Python TabError: Mixed Tabs and Spaces in Indentation
TabError shows up when one block of code is indented with both tabs and spaces. On screen the two look identical, so you cannot find the bug by reading the file. Here is how to render whitespace, convert the file in one command, and configure the editor so it never happens again.
Strings and formatting
2 articlesValueError: could not convert string to float in Python
ValueError: could not convert string to float almost always means one thing: float() got a string with a decimal comma such as 77,59. This walks through the comma, the empty string, the invisible Excel BOM and the non-breaking space, and builds a price parser that survives dirty data.
How to Remove a File Extension From a String in Python
rstrip('.txt') does not remove an extension: it chews a character set, so report.txt silently becomes repor. Here is the mechanism plus four correct approaches — removesuffix, os.path.splitext, Path.stem and slicing. Edge cases too: archive.tar.gz, .gitignore and extensionless files.
Lists and slicing
2 articleslist indices must be integers or slices, not str — Fix
TypeError: list indices must be integers or slices, not str means you indexed a list with a string key. The usual cause is JSON: parsing returned a list of dictionaries, so data['name'] has to become data[0]['name']. Inside: the one-line diagnosis, the mirror error string indices must be integers, and how it differs from KeyError and IndexError.
Deleting From a List While Looping Skips Items in Python
A for loop walks a list by an internal index while remove() and del shift the tail left, so one element slips past the check after every deletion. Nothing is raised and the list comes back half-filtered. Here is the mechanism step by step plus three fixes that work.
Loops and iteration
1 articleErrors and exceptions
1 articleFiles and data
2 articlesPython UnicodeDecodeError Reading a File: cp1251 vs UTF-8
Mojibake instead of Cyrillic and a crash with UnicodeDecodeError are the same bug: Python assumed the wrong encoding. Why open() has no fixed default, how to read the whole error message, and how utf-8-sig differs from utf-8. Every snippet runs as written.
Python JSON: Cyrillic Saved as Unicode Escapes — Fix
Your JSON file stores the word for coffee as \u043a\u043e\u0444\u0435, which is default json.dump behaviour rather than damage. Here is what ensure_ascii controls, what encoding controls, and why you need both at once.