How to use special characters in Abaqus scripting

Out of the anglosaxon world it is very common using the so called special characters. For example, some of the most common special characters in latin-based languages are accented vowels (á, à, ä, â…) and others like ñ, ç, ß…

In general, when working with international software, best practices recommend avoid using special characters and stick to the first 128 characters of the ASCII table (unextended). Otherwise, the program may not recognize those characters and may behave unexpectedly.

1. Special characters in Abaqus

Abaqus does not support directly using special characters, that is, we cannot call our model “máquina” or “sphère“, because it will not recognize the characters “á” and “è“. Similarly, special characters cannot be used when naming any sort of object like: parts, sets, surfaces, materials, sections, instances, jobs

However, we can use directories or paths that contain special characters. Let’s say that we have a Python script in which we create a folder that will be used as working directory:

				
					# Usual imports to run Abaqus
from abaqus import *
from abaqusConstants import *
from caeModules import *
from driverUtils import executeOnCaeStartup
import os
executeOnCaeStartup()
 
# Create new directory and use it as working directory
wdir = 'Análisis_máquina_año2020'
 
# Create folder if it does not exist yet
if not os.path.exists(wdir):
    os.makedirs(wdir)
 
# Set working directory in Abaqus
os.chdir(wdir)

				
			

In this script, we create a new folder (if it doesn’t exist yet). Then, we set that folder as the working directory in Abaqus. But this script will not work because the string wdir contains special characters (á and ñ). To solve it, we must add two features:

				
					# -*- coding: utf-8 -*-
 
# Usual imports to run Abaqus
from abaqus import *
from abaqusConstants import *
from caeModules import *
from driverUtils import executeOnCaeStartup
import os
executeOnCaeStartup()
 
# Create new directory and use it as working directory
wdir = u'Análisis_máquina_año2020'
 
# Create folder if it does not exist yet
if not os.path.exists(wdir):
os.makedirs(wdir)
 
# Set working directory in Abaqus
os.chdir(wdir)
				
			

First, we specify the utf-8 encoding to the Python interpreter (on line 1), that is, we can include Unicode-type characters in the script (which includes these special characters).

Secondly, when creating the string wdir, we indicate that Unicode characters are used by typing u right before the first single quote of the string. The use of single quotes or double quotes does not make any difference.

With these modifications, the script will work as expected.

2. Defining directories in Windows with Python

When working with Windows directories in Python scripts, not only for Abaqus. we need to use the backslash character ‘\’. Nevertheless, the backslash character in Python is reserved to represent ‘escape characters‘ in strings, which are characters with some special functionality in Python.

Escape characters start with a backslash followed by another character, for example, the newline (\n), double quotes (\”), single quotes (\’) or even the backslash itself (\\).

Therefore, to represent a Windows directory in Python we can do the following:

Therefore, to represent a Windows directory in Python we can do the following:

				
						
wdir = u'D:\\ABAQUS\\Análisis_máquina_año2020'
				
			

Wherever there should be the \ symbol, we have to type \\.

We could also indicate that it is a ‘raw string’ by typing before the string (as we did with the u). In that case, escape characters are ignored and considered as single characters. Then we could write:

				
					# Raw string
wdir = ru'D:\ABAQUS\Análisis_máquina_2020'

# Equivalent regular string
wdir2 = u'D:\\ABAQUS\\Análisis_máquina_2020'
				
			

3. What else can we do with Python in Abaqus?

I invite you to read this blog post, where you will discover how to take advantage of Python to generate your sketches in Abaqus automatically! In a fraction of a second, your sketch will be done, and that’s just the beginning.

I hope that you find these tips useful!

Leave a Comment

Scroll to Top