Python: How to replace one or multiple characters in a string?

Problem You have a string which you might have read from an external file. The string contains a significant amount of characters that you would like to automatically replace. Possible Solutions Replacing a single character with another Here’s the output: Switch character in string in list In this example we’ll replace all occurrences of multiple … Read more

How to fix the module not found import error for urllib2?

In today’s post we would like to show how to easily fix error messages when importing the urllib2 library. The solution is pretty much applicable to any Python development tool of choice: Jupyter Notebook, PyChram, Visual Studio IDE and so forth. What is urllib2? Urllib2 is a Python library that works alongside Python2.X versions. It … Read more

How to save a Python Numpy array to a text file?

In today’s tutorial we’ll show you two ways to export and save a Numpy array to a text or csv file which you can then further process: Using the Numpy function np.savetxt Using the Pandas library Create example data We’ll start by creating a simple array: Note: Numpy and Pandas are installed by default with … Read more

How to write Python lists to txt and csv files?

In today’s tutorial we’ll learn to import Python lists into files. We’ll cover three main use cases: Import list into a new file (could be txt, csv, json or others). Append a list to an existing file. Write multiple lists to a file Write a list of dictionaries to a file Save list to a … Read more

How to convert a list to a dictionary in Python?

Problem To convert a simple or nested list into a dictionary data object using Python. Solution In this tutorial we’ll focus on usage of dictionary comprehensions in order to add the content of a Python list iterator into a dictionary. 1. Dict from Python list Let’s assume that we have the following list: We can … Read more

How to remove newlines from a string in Python 3?

Problem We would like to delete automatically appended newlines added to a string using Python 3 code. Solution We have two methods to delete the newline from a string. In this post we’ll discuss each technique and obviously post the required code. Using rstrip() method: The rstrip() method removes any trailing character at the end … Read more

How to print a string in reverse in Python 3?

Problem We would like to turn upside down a string and print it in reverse mode in Python. Solution We have many different methods to approach to reverse a string in Python. Let’s look at each technique one by one in this post. Using Extended Slicing technique: We can obtain with the reverse of a … Read more

How to open and read an excel file with Python?

Problem Working with spreadsheets is essential when dealing with data. In this tutorial we would like to show you how to open and read excel worksheets (xls / xlsx file formats) using Python 3. Solution We have two libraries in Python that can help us to work with the excel sheet. We will discuss more … Read more