Table Of Contents

Previous topic

Extract a spectrum to a CSV file

Next topic

Create an Elemental ID x-ray condition

Use alternative language

Although ASCII characters are preferred throughout the HMSA specifications, it is possible to provide information in Unicode character and alternative spelling.

In pyHMSA, this is done through a special object type called langstr. This object behaves exactly like the default Python’s str type with the exception that alternative spelling can be provided. Let’s look at an example how to specify an author’s name in two different languages.

First we create a data file object and import the langstr type.

from pyhmsa.datafile import DataFile
from pyhmsa.type.language import langstr
datafile = DataFile()

Then we create a new langstr object for the author’s name. The first argument of langstr is the name in English (i.e. ASCII characters). The second argument is a dictionary where the key is a valid language code and/or country code, as specified by ISO 639-1 and ISO 3166, respectively.

author = langstr('Wilhelm Conrad Roentgen', {'de': u'Wilhelm Conrad Röntgen'})
datafile.header.author = author

The alternative spellings of a string can be access using the attribute alternatives which returns a dictionary. Note that once created a langstr object is immutable; it cannot be modified.

print(datafile.header.author.alternatives['de']) # Returns ...

Full source code

#!/usr/bin/env python

from pyhmsa.datafile import DataFile
from pyhmsa.type.language import langstr
datafile = DataFile()

author = langstr('Wilhelm Conrad Roentgen', {'de': u'Wilhelm Conrad Röntgen'})
datafile.header.author = author

print(datafile.header.author.alternatives['de']) # Returns ...