Introduction Welcome to a new code snippet post. Today, we are going to demonstrate how to read a text file line by line in Python. There are various ways to do that, however we are going to show the recommended method specially when dealing with large files. Let us get started… Using with statement A
Introduction In this post, I am going to provide a summary of using quotes in Python. It can be confusing to beginners but it should be easy to grasp by just looking at the following bullet points… Quotes in Python Strings literals in Python must be enclosed with single or double or triple quotes It
Introduction First, keep in mind that null is not part of the Python programming language syntax, though you can find it in other programming languages (ex. Java, JavaScript). In Python syntax, it is None. So what is None ? Do we really need it ? how is it used ? let us discuss that… None
Introduction Imagine that we have two or more dictionaries and we want to combine them into a single dictionary. How can we do that in Python? How do we handle collision when keys are the same? Let us explore that… Code snippets Consider the following example… We can merge the two dictionaries into a third
Introduction Given an input list of strings and a search keyword. We want to check if the search keyword is found in the input list (not necessarily an exact match). In other words, if the search keyword is a substring of at least one string in the input list then it is considered a match.
Problem Given a string of comma separated numbers, we want to convert that string to a Python list of integers Solution The idea here is to split the string into tokens then convert each token to an integer. We can do that in a couple of ways. Let us see how… Use a list comprehension
Introduction Quotes are special characters in Python used to represent string data type. If a single or double quote needs to be part of a string, we can do that in a couple of ways. Let us explore that… Code snippets Use single quotes if you want to add double quotes and visa versa. Here
Introduction The vi editor (available on all Unix flavors) might not be as powerful as some GUI editors such as Sublime Text however there are situations where it is the only available option. Anyway, vi is indeed a powerful command based text editor specially if we master the supported commands. Let us now answer the
Introduction Removing blank lines from a file requires two steps. First, we need to loop through the file lines. Second, check if the line is empty. In Python, there are different ways to perform the loop and check. Let us summarize that… Loop method There are multiple styles to iterate through file lines. Here are
Introduction Removing empty lines in a text file from the Unix shell is quite easy. There are a couple of ways to do that. Let us explore a few… Remove empty lines using sed command The stream editor (sed) can be used to filter and transform text. Removing empty lines using sed can be done