Categories
Python Answers

How to find the nth occurrence of substring in a string with Python?

Sometimes, we want to find the nth occurrence of substring in a string with Python.

In this article, we’ll look at how to find the nth occurrence of substring in a string with Python.

How to find the nth occurrence of substring in a string with Python?

To find the nth occurrence of substring in a string with Python, we can use the re.finditer method.

For instance, we write

import re

s = "ababdfegtduab"
indexes = [m.start() for m in re.finditer(r"ab", s)]

to create a list of indexes with the matches we’re looking for in string s by calling re.finditer with the regex pattern we’re looking for and s.

Then we call m.start to get the index of a instance of the match in the loop.

Conclusion

To find the nth occurrence of substring in a string with Python, we can use the re.finditer method.

Categories
Python Answers

How to speed up bulk insert to MS SQL Server using pyodbc with Python?

Sometimes, we want to speed up bulk insert to MS SQL Server using pyodbc with Python.

In this article, we’ll look at how to speed up bulk insert to MS SQL Server using pyodbc with Python.

How to speed up bulk insert to MS SQL Server using pyodbc with Python?

To speed up bulk insert to MS SQL Server using pyodbc with Python, we can use the executemany method.

For instance, we write

cnxn = pyodbc.connect(conn_str, autocommit=True)
crsr = cnxn.cursor()
crsr.execute("TRUNCATE TABLE fast_executemany_test")

crsr.fast_executemany = True

sql = "INSERT INTO fast_executemany_test (txtcol) VALUES (?)"
params = [(f'txt{i:06d}',) for i in range(1000)]
crsr.executemany(sql, params)

to create a cursor object with cursor.

Then we set the cursor’s fast_executemany property to True to speed up execution.

Then we call executemany with the sql string and the params list to run all the queries.

Conclusion

To speed up bulk insert to MS SQL Server using pyodbc with Python, we can use the executemany method.

Categories
Python Answers

How to construct a timedelta object from a simple string with Python?

Sometimes, we want to construct a timedelta object from a simple string with Python.

In this article, we’ll look at how to construct a timedelta object from a simple string with Python.

How to construct a timedelta object from a simple string with Python?

To construct a timedelta object from a simple string with Python, we can use datetime’s strptime to parse the string.

For instance, we write

from datetime import datetime, timedelta

t = datetime.strptime("05:20:25","%H:%M:%S")
delta = timedelta(hours=t.hour, minutes=t.minute, seconds=t.second)

to call strptime to parse the time string into a datetime object hours, minutes, and seconds.

Then we call timedelta with the hours, minutes, and seconds that we get from the t object to create the timedelta object.

Conclusion

To construct a timedelta object from a simple string with Python, we can use datetime’s strptime to parse the string.

Categories
Python Answers

How to change Google Chrome user agent in Selenium in Python?

Sometimes, we want to change Google Chrome user agent in Selenium in Python.

In this article, we’ll look at how to change Google Chrome user agent in Selenium in Python.

How to change Google Chrome user agent in Selenium in Python?

To change Google Chrome user agent in Selenium in Python, we can use the fake_useragent library.

To install it, we run

pip install fake-useragent

Then we write

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent

options = Options()
ua = UserAgent()
userAgent = ua.random

options.add_argument(f'user-agent={userAgent}')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\ChromeDriver\chromedriver_win32\chromedriver.exe')
driver.get("https://www.example.com")
driver.quit()

to create a UserAgent object.

We get a random user agent with ua.random.

And then we call add_argument to add the userAgent into the options object.

Then we create the Chrome webdriver withj the chrome_options set to options to open Chrome with the user agent we set.

Conclusion

To change Google Chrome user agent in Selenium in Python, we can use the fake_useragent library.

Categories
Python Answers

How to load big JSON files in a memory efficient and fast way with Python?

Sometimes, we want to load big JSON files in a memory efficient and fast way with Python.

In this article, we’ll look at how to load big JSON files in a memory efficient and fast way with Python.

How to load big JSON files in a memory efficient and fast way with Python?

To load big JSON files in a memory efficient and fast way with Python, we can use the ijson library.

To install it, we run

pip install ijson

Then we use it by writing

import ijson

for prefix, the_type, value in ijson.parse(open(json_file_name)):
    print(prefix, the_type, value)

We call ijson.parse to parse the file opened by open.

Then we print the key prefix, data type of the JSON value store in the_type, and the value of the entry with the given key prefix.

Conclusion

To load big JSON files in a memory efficient and fast way with Python, we can use the ijson library.