String manipulation python script

Yan 9 years ago in Scripting 0

Like many of you, many of our in house experts are learning Python in order to increase the power and the flexibility of Micromine. We got a request for a script that I thought was worth sharing with everyone :

Here is a suggestion for a nice Python script that one of our pyxperts could produce:
Change the capitalisation of the text in a file field. Capitalisation options would be:
2. all lower case
3. Proper Case (first letter of each word capitalised)
4. Sentence case (only first letter of entire string capitalised)

With the power of python string manipulation, the formatting is quite easy to do:

  1. s.upper() #where s is a string
  2. s.lower()
  3. string.capwords(s) #where string is a default python module imported using import string
  4. string.capwords(s,'. ')

The only thing left to do is wrap this in a class:

#class that will handle the formatting
class Formatter():
def format(self, format_string,type):
if type == 1:
format_string = format_string.upper()
if type == 2:
format_string = format_string.lower()
if type == 3:
format_string = string.capwords(format_string)
if type == 4:
format_string = string.capwords(format_string,'. ')
return format_string

and modify the field of a Micromine file using the MMpy module.

The following screenshot shows the result on labels of a Dat file

Image 50

In the script there is 2 variables you can edit:

  • field_name_to_format is an array with the name of the fields to format
  • formatting_type is the type of formatting that you want

Here is the script : StringManipScript.py

And the dat file : MineralOccurrence.DAT

If you have any questions or comments, feel free to post them here, and if you like the script, please upvote this post :)

Happy coding !