Categories
Python Answers

How to generating permutations with repetitions with Python?

Sometimes, we want to generating permutations with repetitions with Python.

In this article, we’ll look at how to generating permutations with repetitions with Python.

How to generating permutations with repetitions with Python?

To generating permutations with repetitions with Python, we can compute the cartesian product with itertools.product.

For instance, we write

import itertools

x = [1, 2, 3, 4, 5, 6]
prod = [p for p in itertools.product(x, repeat=2)]

to call itertools.product with list x and repeat set to 2 to get all permutations of any 2 items in list x with repetition.

Then we put the items in a list and assign it to prod.

Conclusion

To generating permutations with repetitions with Python, we can compute the cartesian product with itertools.product.

Categories
Python Answers

How to do equation parsing in Python?

Sometimes, we want to do equation parsing in Python.

In this article, we’ll look at how to do equation parsing in Python.

How to do equation parsing in Python?

To do equation parsing in Python, we can use the compiler module.

For instance, we write

import compiler

eq = "sin(x)*x**2"
ast = compiler.parse(eq)

to call compiler.parse with eq to return an abstract syntax tree object of the given expression in string eq.

Conclusion

To do equation parsing in Python, we can use the compiler module.

Categories
Python Answers

How to extract part of a regex match with Python?

Sometimes, we want to extract part of a regex match with Python.

In this article, we’ll look at how to extract part of a regex match with Python.

How to extract part of a regex match with Python?

To extract part of a regex match with Python, we can use the := operator.

For instance, we write

pattern = '<title>(.*)</title>'
text = '<title>hello</title>'

if match := re.search(pattern, text, re.IGNORECASE):
  title = match.group(1)

to call re.searcg with the pattern to find the pattern in the text.

We use re.IGNORECASE to find case insensitive matches.

Then we get the match object by using the := with the results returned by re.search.

And we call match.group to find the match for the capturing group.

Conclusion

To extract part of a regex match with Python, we can use the := operator.

Categories
Python Answers

How to do Timezone conversion with Python?

Sometimes, we want to do Timezone conversion with Python.

In this article, we’ll look at how to do Timezone conversion with Python.

How to do Timezone conversion with Python?

To do Timezone conversion with Python, we can use the pytz module.

For instance, we write

import datetime
import pytz

dt_today = datetime.datetime.today()
dt_london = dt_today.astimezone(pytz.timezone('Europe/London'))

to call datetime.datetime.today to get today’s datetime.

Then we convert the datetime to the London time zone with the astimezone method called with the time zone returned by pytz.timezone called with the time zone string.

Conclusion

To do Timezone conversion with Python, we can use the pytz module.

Categories
Python Answers

How to extract text from MS word files in Python?

Sometimes, we want to extract text from MS word files in Python.

In this article, we’ll look at how to extract text from MS word files in Python.

How to extract text from MS word files in Python?

To extract text from MS word files in Python, we can use the zipfile library.

For instance, we write

import zipfile, re

docx = zipfile.ZipFile('/path/to/file/mydocument.docx')
content = docx.read('word/document.xml').decode('utf-8')
cleaned = re.sub('<(.|\n)*?>','',content)
print(cleaned)

to create ZipFile object with the path string to the Word file.

Then we call read with 'word/document.xml' to read the Word file.

And we call decode to decode the text as Unicode.

Next, we call re.sub to replace the tags with empty strings.

Conclusion

To extract text from MS word files in Python, we can use the zipfile library.