Categories
Python Answers

How to remove empty strings from a list of strings with Python?

Sometimes, we want to remove empty strings from a list of strings with Python.

In this article, we’ll look at how to remove empty strings from a list of strings with Python.

How to remove empty strings from a list of strings with Python?

To remove empty strings from a list of strings with Python, we can call the filter function.

For instance, we write

str_list = list(filter(None, str_list))

to call filter with None and str_list to return an iterator with the strings in str_list that aren’t empty.

Then we convert the iterator to a list with list.

Conclusion

To remove empty strings from a list of strings with Python, we can call the filter function.

Categories
Python Answers

How to retrieve a module’s path with Python?

Sometimes, we want to retrieve a module’s path with Python.

In this article, we’ll look at how to retrieve a module’s path with Python.

How to retrieve a module’s path with Python?

To retrieve a module’s path with Python, we can call os.path.abspath.

For instance, we write

import os

path = os.path.abspath(a_module.__file__)

to call os.path.abspath with a_module.__file__ to get the absolute path of the a_module module.

Conclusion

To retrieve a module’s path with Python, we can call os.path.abspath.

Categories
Python Answers

How to split a string by a delimiter in Python?

Sometimes, we want to split a string by a delimiter in Python

In this article, we’ll look at how to split a string by a delimiter in Python.

How to split a string by a delimiter in Python?

To split a string by a delimiter in Python, we can use the csv module.

For instance, we write

import csv

csv.register_dialect("myDialect", delimiter = "__")
lines = ["MATCHES__STRING"]

for row in csv.reader(lines):
   # ...

to callcsv.register_dialect to register a dialect with the delimiter for the CSV file.

Then we call csv.reader with lines to split the strings in lines into the row list by the "__" string.

Conclusion

To split a string by a delimiter in Python, we can use the csv module.

Categories
Python Answers

How to append existing Excel sheet with new dataframe using Python Pandas?

Sometimes, we want to append existing Excel sheet with new dataframe using Python Pandas.

In this article, we’ll look at how to append existing Excel sheet with new dataframe using Python Pandas.

How to append existing Excel sheet with new dataframe using Python Pandas?

To append existing Excel sheet with new dataframe using Python Pandas, we can use ExcelWriter.

For instance, we write

import pandas as pd
import openpyxl

workbook = openpyxl.load_workbook("test.xlsx")
writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')
writer.book = workbook
writer.sheets = dict((ws.title, ws) for ws in workbook.worksheets)
data_df.to_excel(writer, 'Existing_sheetname')
writer.save()
writer.close()

to create an ExcelWriter object with

writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')

We add the workbook with

writer.book = workbook

Then we add the sheets with

writer.sheets = dict((ws.title, ws) for ws in workbook.worksheets)

And then we add the dataframe to the Excel file with

data_df.to_excel(writer, 'Existing_sheetname')

Conclusion

To append existing Excel sheet with new dataframe using Python Pandas, we can use ExcelWriter.

Categories
Python Answers

How to use zip and that pads to longest length with Python?

Sometimes, we want to use zip and that pads to longest length with Python.

In this article, we’ll look at how to use zip and that pads to longest length with Python.

How to use zip and that pads to longest length with Python?

To use zip and that pads to longest length with Python, we can use the itertools.zip_longest method.

For instance, we write

l = list(itertools.zip_longest(a, b, c))

to call itertools.zip_longest with lists a, b, and c to return an iterable with tuples that has the entries of each list at the given position.

If the value doesn’t exist, it’s filled with None.

Then we convert the iterable to a list with list.

Conclusion

To use zip and that pads to longest length with Python, we can use the itertools.zip_longest method.