How to Ignore Case in Python Strings

Problem Formulation and Solution Overview In this article, you’ll learn how to ignore (upper/lower/title) case characters when dealing with Strings in Python. The main reason for ignoring the case of a String is to perform accurate String comparisons or to return accurate search results. For example, the following three (3) Strings are not identical. If … Read more

How to Convert Tab-Delimited File to CSV in Python?

The easiest way to convert a tab-delimited values (TSV) file to a comma-separated values (CSV) file is to use the following three lines of code: import pandas as pd df = pd.read_csv(‘my_file.txt’, sep=’\t’, header=None) df.to_csv(‘my_file.csv’, header=None) We’ll explain this and other approaches in more detail next—scroll down to Method 3 for this exact method. Problem … Read more

How to Sort Words Alphabetically in Python

Problem Formulation and Solution Overview In this article, you’ll learn how to sort words alphabetically in Python. To make it more fun, we have the following running scenario: Some English sayings, also known as Tongue-Twisters, are fun to try and say quickly. They are also used to improve non-English speaking individuals and small children’s fluency … Read more

Python CSV to UTF-8

This article concerns the conversion and handling of CSV file formats in combination with the UTF-8 encoding standard. πŸ’‘ The Unicode Transformation Format 8-Bit (UTF-8) is a variable-width character encoding used for electronic communication. UTF-8 can encode more than 1 million (more or less weird) characters using 1 to 4 byte code units. Example UTF-8 … Read more

How to Convert a Text File to a CSV File in Python?

You can convert a text file to a CSV file in Python in four simple steps: (1) Install the Pandas library, (2) import the Pandas library, (3) read the CSV file as DataFrame, and (4) write the DataFrame to the file: (Optional in shell) pip install pandas import pandas as pd df = pd.read_csv(‘my_file.txt’) df.to_csv(‘my_file.csv’, … Read more

Python RegEx – Match Whitespace But Not Newline

Problem Formulation πŸ’¬ Challenge: How to design a regular expression pattern that matches whitespace characters such as the empty space ‘ ‘ and the tabular character ‘\t’, but not the newline character ‘\n’? An example of this would be to replace all whitespaces (except newlines) between a space-delimited file with commas to obtain a CSV. … Read more

How to Convert Space-Delimited File to CSV in Python?

The easiest way to convert a space-delimited file to a comma-separated values (CSV) file is to use the following three lines of code: import pandas as pd df = pd.read_csv(‘my_file.txt’, sep=’\s+’, header=None) df.to_csv(‘my_file.csv’, header=None) We’ll explain this and other approaches in more detail next—scroll down to Method 3 for this exact method. Problem Formulation Given … Read more

How to Repeat a String Multiple Times in Python

Problem Formulation and Solution Overview In this article, you’ll learn how to repeat a string multiple times in Python. Over your career as a Python coder, you will encounter situations when a string needs to be output/displayed a specified number of times. The examples below offer you various ways to accomplish this task. πŸ’¬ Question: … Read more