Parsing examples

Some examples on parsing to and from supported formats. More info about all parsing methods can be found in the manual section.

Declare paths

First, let's do all the necessary imports and generate the paths that we'll use for file import and export. We'll then use the util_get_file_paths function to get the file paths in those directories.


In [1]:
import os
import pygaps

# Get directory paths
xl_path = os.path.join(os.getcwd(), 'data', 'parsing', 'excel')
json_path = os.path.join(os.getcwd(), 'data', 'parsing', 'json')
csv_path = os.path.join(os.getcwd(), 'data', 'parsing', 'csv')

# Find files
json_file_paths = pygaps.util_get_file_paths(json_path, '.json')
xl_file_paths = pygaps.util_get_file_paths(xl_path, '.xls')
csv_file_paths = pygaps.util_get_file_paths(csv_path, '.csv')

JSON Parsing

JSON Import


In [2]:
# Import them
isotherms = []
for filepath in json_file_paths:
    with open(filepath, 'r') as text_file:
        isotherms.append(pygaps.isotherm_from_json(text_file.read()))

# See the files
for isotherm in isotherms:
    print(isotherm)


Material: HKUST-1(Cu)
Batch: Test
Adsorbate used: carbon dioxide
Isotherm temperature: 303.0K
iso_type: Calorimetrie
Units: 
	Unit for loading: mmol/g
	Unit for pressure: bar
Other properties: 
	comment: None
	date: 2010-05-21 00:00:00
	is_real: True
	lab: MADIREL
	machine: CV
	project: None
	t_act: 150.0
	user: ADW

Material: MCM-41
Batch: Test
Adsorbate used: nitrogen
Isotherm temperature: 77.0K
iso_type: Isotherme
Units: 
	Unit for loading: mmol/g
	Unit for pressure: bar
Other properties: 
	comment: None
	date: None
	is_real: True
	lab: MADIREL
	machine: Triflex
	project: None
	t_act: 150.0
	user: PI

JSON Export


In [3]:
for isotherm in isotherms:
    filename = os.path.join(json_path, 
                            ' '.join([isotherm.material,
                                      str(isotherm.adsorbate),
                                      str(isotherm.temperature)]) + '.json')
    with open(filename, mode='w') as f:
        f.write(pygaps.isotherm_to_json(isotherm))

Excel Parsing

Excel does not have to be installed on the system in use.

Excel Import


In [4]:
# Import them
[print(path) for path in xl_file_paths]
isotherms = [pygaps.isotherm_from_xl(path) for path in xl_file_paths]

# See the files
for isotherm in isotherms:
    print(isotherm)


C:\Users\Paul\Git\pyGAPS\docs\examples\data\parsing\excel\HKUST-1(Cu) CO2 303.0.xls
C:\Users\Paul\Git\pyGAPS\docs\examples\data\parsing\excel\MCM-41 N2 77.0.xls
Material: HKUST-1(Cu)
Batch: Test
Adsorbate used: carbon dioxide
Isotherm temperature: 303.0K
iso_type: Calorimetrie
Units: 
	Unit for loading: mmol/g
	Unit for pressure: bar
Other properties: 
	date: 2010-05-21 00:00:00
	is_real: True
	lab: MADIREL
	machine: CV
	project: None
	t_act: 150.0
	user: ADW
	comment: None

Material: MCM-41
Batch: Test
Adsorbate used: nitrogen
Isotherm temperature: 77.0K
iso_type: Isotherme
Units: 
	Unit for loading: mmol/g
	Unit for pressure: bar
Other properties: 
	comment: None
	date: None
	is_real: True
	lab: MADIREL
	machine: Triflex
	project: None
	t_act: 150.0
	user: PI

Excel Export


In [5]:
# Export each isotherm in turn
for isotherm in isotherms:
    filename = ' '.join([isotherm.material, str(isotherm.adsorbate), str(isotherm.temperature)]) + '.xls'
    pygaps.isotherm_to_xl(isotherm, os.path.join(xl_path, filename))

CSV Parsing

CSV Import


In [6]:
# Import them
isotherms = [pygaps.isotherm_from_csv(path) for path in csv_file_paths]

# See the files
for isotherm in isotherms:
    print(isotherm)


Material: HKUST-1(Cu)
Batch: Test
Adsorbate used: carbon dioxide
Isotherm temperature: 303.0K
iso_type: Calorimetrie
Units: 
	Unit for loading: mmol/g
	Unit for pressure: bar
Other properties: 
	user: ADW
	machine: CV
	date: 21/05/2010 00:00
	is_real: True
	t_act: 150.0
	lab: MADIREL

Material: MCM-41
Batch: Test
Adsorbate used: nitrogen
Isotherm temperature: 77.0K
iso_type: Isotherme
Units: 
	Unit for loading: mmol/g
	Unit for pressure: bar
Other properties: 
	user: PI
	machine: Triflex
	is_real: True
	t_act: 150.0
	lab: MADIREL

CSV Export


In [7]:
# Export each isotherm in turn
for isotherm in isotherms:
    filename = ' '.join([isotherm.material, str(isotherm.adsorbate), str(isotherm.temperature)]) + '.csv'
    pygaps.isotherm_to_csv(isotherm, os.path.join(csv_path, filename))