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

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 Find All Matches using Regex

Problem Formulation and Solution Overview In this article, you’ll learn how to find all matches in a string using regex. The Regular Expression, also referred to as regex, is a complex pattern to search for and locate matching character(s) within a string. At first, this concept may seem daunting, but with practice, regex will improve … Read more

How to Replace Whitespaces with Underscores

Problem Formulation and Solution Overview In this article, you’ll learn how to replace whitespaces with underscores in Python. To make it more fun, we have the following running scenario: Bryan, an IT Instructor, has given his students a Python coding challenge: Take a famous quote and replace all whitespaces with underscores in four (4) ways: … Read more

You Cannot Use Python Regex in startswith(). Do This Instead.

I’m sitting in front of my computer refactoring Python code and have just thought of the following question: Can You Use a Regular Expression with the Python string.startswith() Method? The short answer is no. The string.startswith() method doesn’t allow regular expression inputs. And you don’t need it because regular expressions can already check if a … Read more

Python | List All Occurrences of Pattern in String

πŸ’¬ Question: Which method finds the list of all occurrences of the pattern in the given string? Problem Formulation Problem Formulation: Given a longer string and a shorter string. How to find all occurrences of the shorter string in the longer one? Consider the following example: Longer string: ‘Finxters learn Python with Finxter’ Shorter string … Read more

How to Extract Numbers from a String

Problem Formulation and Solution Overview In this article, you’ll learn how to extract numbers from a string in Python. To make it more fun, we have the following running scenario: This article references an Albanian proverb written by Driton Selmani in 2012. We will leave the interpretation up to you. πŸ’¬ Question: How would we … Read more

How to Suppress Scientific Notation in Python

[toc] Summary: Use the string literal syntax f”{number:.nf}” to suppress the scientific notation of a number to its floating-point representation. Problem Formulation: How will you suppress a number represented in scientific notation by default to a floating-point value? Note: Generally, Python represents huge floating-point numbers or very small floating-point numbers in their scientific form. Scientific … Read more