Most answered questions in Python

0 votes
1 answer

Importing modules from parent folder

It seems that the problem is not ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
889 views
0 votes
1 answer

ImportError: Import by filename is not supported

Instead of doing a import like __import__ you can ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
2,488 views
0 votes
1 answer

Does array element contain substring?

Loop through the $forbiddennames array and use stripos to check if ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
384 views
0 votes
1 answer

How do I check if a string contains a specific word?

You can use the strpos() function which is used ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
1,110 views
0 votes
1 answer

How to replace all occurrences of a string?

The general pattern is str.split(search).join(replacement) This used to be ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
644 views
0 votes
1 answer

How to convert bytes to a string?

You need to decode the bytes object ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
363 views
0 votes
1 answer

How to compare two string with some characters only in python?

You could use in to check if a string ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
586 views
0 votes
1 answer

How to Test a string for a substring?

if "ABCD" in "xxxxABCDyyyy": ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
360 views
0 votes
1 answer

How to determine whether a substring is in a different string [duplicate]

with in: substring in string: >>> substring = "please help ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
409 views
0 votes
1 answer

How do I check if a given Python string is a substring of another one?

Try using in like this: >>> x = 'hello' >>> y ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
375 views
0 votes
1 answer

Does Python have a string 'contains' substring method?

You can use the in operator: if "blah" not in ...READ MORE

Nov 26, 2020 in Python by Gitika
• 65,910 points
309 views
0 votes
1 answer

How can I list txt files into the folder?

Hello @Mziko You can refer this:https://datatofish.com/txt-files-d ...READ MORE

Nov 26, 2020 in Python by Niroj
• 82,880 points
410 views
0 votes
1 answer

Import a module from a relative path?

Assuming that both your directories are real ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
1,257 views
0 votes
1 answer

How can I safely create a nested directory?

On Python ≥ 3.5, use pathlib.Path.mkdir: from pathlib import ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
362 views
0 votes
1 answer

How to import a module given the full path?

For Python 3.5+ use: import importlib.util spec = importlib.util.spec_from_file_location("module.name", ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
2,082 views
0 votes
1 answer

Calling a function of a module by using its name (a string).

Assuming module foo with method bar: import foo method_to_call = getattr(foo, 'bar') result ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
405 views
0 votes
1 answer

How to import a module given its name as string?

With Python older than 2.7/3.1, that's pretty ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
847 views
0 votes
1 answer

How to find all occurrences of a substring?

There is no simple built-in string function ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
677 views
0 votes
1 answer

How to match all occurrences of a regex?

Using scan should do the trick: string.scan(/regex/) READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
2,734 views
0 votes
1 answer

How to find the occurrences of any word with more than two vowels in a file using regex?

for word in read: <--- iterating ...READ MORE

Nov 25, 2020 in Python by Gitika
• 65,910 points
2,432 views
0 votes
1 answer

How to merge multiple json objects into a single json object using python

You may do like this but it ...READ MORE

Nov 22, 2020 in Python by Gitika
• 65,910 points
18,694 views
0 votes
1 answer

Python and pip, list all versions of a package that's available?

The script at Pastebin does work. However, ...READ MORE

Nov 22, 2020 in Python by Gitika
• 65,910 points
5,025 views
0 votes
1 answer

Installing specific package versions with pip

TL;DR: pip install -Iv (i.e. pip install -Iv MySQL_python==1.2.2) First, I ...READ MORE

Nov 22, 2020 in Python by Gitika
• 65,910 points
746 views
0 votes
1 answer

How can I install pip on Windows?

Python 2.7.9+ and 3.4+ Python 3.4 (released March 2014) ...READ MORE

Nov 22, 2020 in Python by Gitika
• 65,910 points
440 views
0 votes
1 answer

Python Error: ModuleNotFoundError: No module named '<moduleName>'

Python3 and pip are most likely pointing ...READ MORE

Nov 22, 2020 in Python by Gitika
• 65,910 points
6,539 views
0 votes
1 answer

I am not able to install pywin32 into my Macos ..I tried all the ways and I could not fix the error...can you please help me

Hello @Harikrishna, For your query you can refer ...READ MORE

Nov 23, 2020 in Python by Niroj
• 82,880 points
5,830 views
0 votes
1 answer

invalid literal for int() with base 10 in python

Hello, You can try this simple code: def getSum(n): ...READ MORE

Nov 23, 2020 in Python by Niroj
• 82,880 points
1,886 views
0 votes
1 answer

I've been trying to run this code, but the error says "Expected an indented block" for the line, " word_as_list[index] = guess."

Hi, @Paradox, The error message IndentationError: expected an indented ...READ MORE

Nov 21, 2020 in Python by anonymous
• 65,910 points
865 views
0 votes
1 answer

How to Install pip for python 3.7 on Ubuntu 18?

The command you're looking for is: python3.7 -m ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
8,979 views
0 votes
1 answer

How are Python metaclasses different from regular class inheritance?

Python 3 class MyClass(object): = New-style class class MyClass: = New-style ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
309 views
0 votes
1 answer

How to get the current time in Python?

Use: >>> import datetime >>> datetime.datetime.now() datetime.datetime(2009, 1, 6, 15, ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
450 views
0 votes
1 answer

How do I parse XML in Python?

First build an Element instance root from the XML, ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
614 views
0 votes
1 answer

How do I install a Python package with a .whl file?

I just used the following which was ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
4,827 views
0 votes
1 answer

How to create a zip archive of a directory in Python?

Hii, The easiest way is to use shutil.make_archive. It ...READ MORE

Nov 20, 2020 in Python by Niroj
• 82,880 points
645 views
0 votes
1 answer

How do I update pip itself from inside my virtual environment?

Hello, pip is just a PyPI package like any other; you ...READ MORE

Nov 20, 2020 in Python by Niroj
• 82,880 points
4,724 views
0 votes
1 answer

ImportError: No module named requests

Hello, To install requests module for Python2: $ sudo apt-get install ...READ MORE

Nov 20, 2020 in Python by Niroj
• 82,880 points
670 views
0 votes
1 answer

How can I open multiple files using “with open” in Python?

Hello, Just replace and with , and you're done: try: ...READ MORE

Nov 20, 2020 in Python by Niroj
• 82,880 points
1,304 views
0 votes
1 answer

How to set environment variables in Python?

Hii, Environment variables must be strings, so use os.environ["DEBUSSY"] ...READ MORE

Nov 20, 2020 in Python by Niroj
• 82,880 points
649 views
0 votes
1 answer

Why not inherit from List<T>?

There are some good answers here. I ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
349 views
0 votes
1 answer

How to run code when a class is subclassed?

Classes (by default) are instances of the ...READ MORE

Nov 20, 2020 in Python by Gitika
• 65,910 points
378 views
0 votes
1 answer

I have created python chatbot using flask framework using yml file now i want to use json file what should i do now ?

Hello @golu , Try something like this: with open(yaml_file, 'r') ...READ MORE

Nov 19, 2020 in Python by Niroj
• 82,880 points
1,409 views
0 votes
1 answer

How to get the home directory in Python?

Hello, You need to use os.path.expanduser. This will ensure it works ...READ MORE

Nov 19, 2020 in Python by Niroj
• 82,880 points
1,478 views
0 votes
1 answer

How do I parse XML in Python?

Hello @kartik, You can use BeautifulSoup: from bs4 import BeautifulSoup x="""<foo> ...READ MORE

Nov 19, 2020 in Python by Niroj
• 82,880 points
317 views
0 votes
1 answer

How do I create a constant in Python?

Hello @kartik, No there is not. You cannot ...READ MORE

Nov 19, 2020 in Python by Niroj
• 82,880 points
1,201 views
0 votes
1 answer

How can I safely create a nested directory?

Python 3.5+: import pathlib pathlib.Path('/my/directory').mkdir(parents=True, exist_ok=True) pathlib.Path.mkdir as used above ...READ MORE

Nov 19, 2020 in Python by Gitika
• 65,910 points
2,733 views
0 votes
1 answer

How to call an external command?

Look at the subprocess module in the standard library: import ...READ MORE

Nov 19, 2020 in Python by Gitika
• 65,910 points
373 views
0 votes
1 answer

Speed up millions of regex replacements in Python 3

One thing you can try is to ...READ MORE

Nov 19, 2020 in Python by Gitika
• 65,910 points
1,522 views
0 votes
1 answer

How to use multiprocessing pool.map with multiple arguments?

The answer to this is version- and ...READ MORE

Nov 19, 2020 in Python by Gitika
• 65,910 points
13,756 views
0 votes
1 answer

How do I get the number of elements in a list?

Hello, The len() function can be used with several different ...READ MORE

Nov 18, 2020 in Python by Niroj
• 82,880 points
452 views