Categories
Python Answers

How to input a regex in string.replace with Python?

Sometimes, we want to input a regex in string.replace with Python.

In this article, we’ll look at how to input a regex in string.replace with Python.

How to input a regex in string.replace with Python?

To input a regex in string.replace with Python, we can use the re.sub method.

For instance, we write

import re

line = re.sub(r"</?\[\d+>", "", line)

to call re.sub to replace the characters listed in the regex with empty strings in the line string.

We replace slashes,. questions marks, opening square brackets and digits with empty strings.

Conclusion

To input a regex in string.replace with Python, we can use the re.sub method.

Categories
Python Answers

How to get percentage of total with groupby with Python Pandas?

Sometimes, we want to get percentage of total with groupby with Python Pandas.

In this article, we’ll look at how to get percentage of total with groupby with Python Pandas.

How to get percentage of total with groupby with Python Pandas?

To get percentage of total with groupby with Python Pandas, we can use the transform method.

For instance, we write

df['sales'] / df.groupby('state')['sales'].transform('sum')

to get the total with

df.groupby('state')['sales'].transform('sum')

Then we use that as the divisor for df['sales'] to divide each value in the sales column by the total.

Conclusion

To get percentage of total with groupby with Python Pandas, we can use the transform method.

Categories
Python Answers

How to convert local time string to UTC with Python?

Sometimes, we want to convert local time string to UTC with Python.

In this article, we’ll look at how to convert local time string to UTC with Python.

How to convert local time string to UTC with Python?

To convert local time string to UTC with Python, we can use the astimezone method.

For instance, we write

from datetime import datetime   
import pytz

local = pytz.timezone("America/Los_Angeles")
naive = datetime.strptime("2022-2-3 10:11:12", "%Y-%m-%d %H:%M:%S")
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)

to call pytz.timezone to get the time zone.

Then we parse our datetime string as a naive datetime with

naive = datetime.strptime("2022-2-3 10:11:12", "%Y-%m-%d %H:%M:%S")

Then we convert it to a local time with

local_dt = local.localize(naive, is_dst=None)

And then we convert it to UTC with

utc_dt = local_dt.astimezone(pytz.utc)

Conclusion

To convert local time string to UTC with Python, we can use the astimezone method.

Categories
Python Answers

How to extract file name from path, no matter what the os/path format with Python?

Sometimes, we want to extract file name from path, no matter what the os/path format with Python.

In this article, we’ll look at how to extract file name from path, no matter what the os/path format with Python.

How to extract file name from path, no matter what the os/path format with Python?

To extract file name from path, no matter what the os/path format with Python, we can use the os.path.basename method.

For instance, we write

import os

print(os.path.basename(your_path))

to get the file name from the your_path path string with os.path.basename.

Conclusion

To extract file name from path, no matter what the os/path format with Python, we can use the os.path.basename method.

Categories
Python Answers

How to make Python exec work with locals?

Sometimes, we want to make Python exec work with locals.

In this article, we’ll look at how to make Python exec work with locals.

How to make Python exec work with locals?

To make Python exec work with locals, we’ve to convert them to globals.

For instance, we write

def foo():
    ldict = {}
    exec("a=3", globals(), ldict)
    a = ldict['a']
    print(a)

to call exec with the code we want to run, globals(), and ldict.

We call globals to turn a into a global variable.

And then we put the variable result into ldict.

Next, we get the value of variable a from ldict with

a = ldict['a']

Conclusion

To make Python exec work with locals, we’ve to convert them to globals.