First time here? Check out the FAQ!

Revision history  [back]

I tried with the following code, but failed to do the trick:

import os
import re
from typing import Any

def strip_ansi(s: str) -> str:
    """Strip ANSI escape sequences from a string."""
    return re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', '', s)

def strip_junk(value: Any) -> Any:
    """Strip unwanted characters or sequences from a value."""
    if isinstance(value, str):
        # Strip ANSI escape sequences
        value = strip_ansi(value)
    # Optionally, you can add more conditions here to handle other types of values
    return value

# Now import pymatgen and other modules
from mp_api.client import MPRester
from pymatgen.io.vasp.sets import MPRelaxSet

# Use MPRester to fetch data
with MPRester() as mpr:
    # Search for materials with chemical system "Au" and space group number 225 (Fm-3m)
    results = mpr.materials.search(chemsys="Au", num_elements=(1, 1), spacegroup_number=225)

    # Process and print results
    for result in results:
        material_id = result.material_id
        formula_pretty = result.formula_pretty
        print(f"Material ID: {strip_junk(material_id)}, Formula: {strip_junk(formula_pretty)}")

        # Get material structure
        structure = mpr.materials.get_structure_by_material_id(material_id)

        # Generate VASP input files using MPRelaxSet
        vasp_input_set = MPRelaxSet(structure)
        incar = vasp_input_set.incar

        # Print INCAR file content
        if 'WINGDB_ACTIVE' in os.environ:
            other_value = strip_junk(incar)
            print(other_value)
        else:
            print(incar)