Categories
Python Answers

How to create a long multi-line string with Python?

Sometimes, we want to create a long multi-line string with Python.

In this article, we’ll look at how to create a long multi-line string with Python.

How to create a long multi-line string with Python?

To create a long multi-line string with Python, we can wrap our string content with 3 single or double quotes.

For instance, we write

s = """ Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. 
Vivamus id tellus at sem euismod bibendum vitae quis odio. 
Aliquam ac convallis risus"""

to create the string s that spans multiple lines by wrapping its content with 3 double quotes.

Conclusion

To create a long multi-line string with Python, we can wrap our string content with 3 single or double quotes.

Categories
Python Answers

How to write to an existing Excel file without overwriting data using Python Pandas?

Sometimes, we want to write to an existing Excel file without overwriting data using Python Pandas.

In this article, we’ll look at how to write to an existing Excel file without overwriting data using Python Pandas.

How to write to an existing Excel file without overwriting data using Python Pandas?

To write to an existing Excel file without overwriting data using Python Pandas, we can use ExcelWriter.

For instance, we write

import pandas
from openpyxl import load_workbook

book = load_workbook('Masterfile.xlsx')
writer = pandas.ExcelWriter('Masterfile.xlsx', engine='openpyxl') 
writer.book = book

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

data_filtered.to_excel(writer, "Main", cols=['Diff1', 'Diff2'])

writer.save()

to create the ExcelWriter instance with the Excel file path.

Load the workbook with

writer.book = book

We load the sheets with

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

Then we write the columns Diff1 and Diff2 to the file with

data_filtered.to_excel(writer, "Main", cols=['Diff1', 'Diff2'])

And then we call save to save the changes.

Conclusion

To write to an existing Excel file without overwriting data using Python Pandas, we can use ExcelWriter.

Categories
Python Answers

How to map function over numpy array with Python?

Sometimes, we want to map function over numpy array with Python.

In this article, we’ll look at how to map function over numpy array with Python.

How to map function over numpy array with Python?

To map function over numpy array with Python, we can use the np.vectorize method.

For instance, we write

import numpy as np
x = np.array([1, 2, 3, 4, 5])
squarer = lambda t: t ** 2
f = np.vectorize(squarer)
y = f(x)

to create the squarer function that returns t raised to the power of 2.

Then we call vectorize with squarer to return a function that we can use on a numpy array to call the function to map all items in the numpy array to the new values.

And then we call f with x to return the numpy array y with the values in x squared.

Conclusion

To map function over numpy array with Python, we can use the np.vectorize method.

Categories
Python Answers

How to measure elapsed time in Python?

Sometimes, we want to measure elapsed time in Python.

In this article, we’ll look at how to measure elapsed time in Python.

How to measure elapsed time in Python?

To measure elapsed time in Python, we can use the default_timer function in the time_it module.

For instance, we write

from timeit import default_timer as timer

start = timer()
# ...
end = timer()
print(end - start)

to import default_timer as timer.

And then we call it to get the start and end times.

Then we get the elapsed time in seconds between the time when the 2 lines is run by subtracting end by start.

The elapsed time is in seconds.

Conclusion

To measure elapsed time in Python, we can use the default_timer function in the time_it module.

Categories
Python Answers

How to play an mp3 with Python pygame?

Sometimes, we want to play an mp3 with Python pygame.

In this article, we’ll look at how to play an mp3 with Python pygame.

How to play an mp3 with Python pygame?

To play an mp3 with Python pygame, we can call the pygame.time.Clock().tick method in the while loop that we use the return value of pygame.mixer.music.get_busy as the condition.

For instance, we write

while pygame.mixer.music.get_busy(): 
    pygame.time.Clock().tick(10)

to keep the clip playing as long as pygame.mixer.music.get_busy returns True, which is when the clip is still being played.

Conclusion

To play an mp3 with Python pygame, we can call the pygame.time.Clock().tick method in the while loop that we use the return value of pygame.mixer.music.get_busy as the condition.