This post teaches you how to call a C++ (cpp) code or function from python. Step 1: Develop a C wrapper for CPP as mentioned below Assume you have a C++ DLL project with CPP files and HPP files Create CLibrary.H and CLibrary.C files and add to C++ DLL project CLibrary.H should contain following code. […]
If you are looking to get current year in integer format in Python programming language, then use below one from datetime import datecurrent_year = date.today().year Source:https://www.quora.com/How-do-I-get-the-current-year-as-an-int-value-in-Python
If you want to remove extra whitespaces from start and end of a string, use strip() method in Python. strip() method removes all default whitespaces (spaces and tabs \t) and returns the copy of the string. Python code to strip trailing whitespaces from string
string = " xyz is equal to xyz " # string with whitespaces (spaces and tabs)
print "string with trailing whitespaces is \"" + string + "\"" # printing string with trailing whitespaces
string = string.strip() # strip trailing whitespaces
print "string after stripping trailing whitespaces is \"" + string + "\"" # printing the string after stripping trailing whitespaces
Note: Code works in both Python 2 and 3 Output […]
Python code to get the position of next or second occurrence of a sub-string within a string.
# Below code finds the position of second occurrence of "xyz"
string = "xyz is equal to xyz"
firstSubPos=string.find("xyz") # get the position of first substring "xyz"
secondSubPos=string.find("xyz", firstSubPos+1) # get the position of second occurence of substring "xyz"
print "position of second occurence of substring \"xyz\" is " + str(secondSubPos) # print the position
Note: Above code works in both Python 2 and Python 3 Output: Read -> Strip trailing whitespaces from string [Python]
This Python code helps you to to replace a string between two delimiters or characters delete/remove string between two characters or delimiters Python code to replace string between two delimiters
# This macro replaces nivas in below string with silveri
string = "sri[nivas]"
firstDelPos=string.find("[") # get the position of [
secondDelPos=string.find("]") # get the position of ]
stringAfterReplace = string.replace(string[firstDelPos+1:secondDelPos], "silveri") # replace the string between two delimiters
print stringAfterReplace # print the string after sub string between dels is replaced
Python code to delete/remove string between two characters/delimiters Replace 5th line of above code with below one
stringAfterReplace = string.replace(string[firstDelPos+1:secondDelPos], "") # remove the string between two delimiters
Note: This code works in both […]
Simple python script to extract sub string between two delimiters of a string. Examples of delimiters are brackets, parentheses, braces, single or double quotes etc. You can also customize this code to extract the string between two special characters or two sub strings. Comments on each line of code explains you to easily understand the code. Note: […]
Below are the easy steps to install and setup python on Windows (I tried with Windows 10). click here to read Python2 or Python3? Download Python from https://www.python.org/downloads/ Install it on your C drive Search PowerShell from Start menu or Windows search box. Click on it to open. Type python in your PowerShell (Terminal) If you get […]
Openpyxl is a python library to perform read/write excel xlsx/xlsm/xltx/xltm files. Below are the simple steps to install openpyxl on Windows Click below link to download openpyxl package Download openpyxl package Goto Navigation – > Download Files Download the openpyxl package by clicking on openpyxl.x.x.x.tar.gz file link (x.x.x is version number) Extract the downloaded TAR […]