pymatgen讀/寫各種文件

pymatgen讀/寫各種文件

 

pymatgen是材料大數據計算的必備程序包了,其也作爲API與materials project對接,可以批量下載自己想要的材料結構、性質,它也提供了大量VASP計算後的數據處理、計算,堪稱材料基因組學的神器!

 

pymatgen提供了讀取CIF、json、POSCAR、CONTCAR等文件的程序,可以批量讀取、轉化、另存。不過,作爲初學者,裏面很多功能是很難理解的,特別是對python語言不熟的同學。

 

1. pymatgen讀取json文件,轉化爲Structure對象,並保存爲CIF文件

from pymatgen import Structure, Lattice, MPRester, Molecule
import json
from pymatgen.io.cif import CifWriter


# 如果只有一個結構
#data_file = open("../db-1.json", "r")
#all_structures = data_file.readlines()
#structure = json.loads(all_structure)["structure"]
structure = Structure.from_dict(structure)
c = CifWriter(structure)
c.write_file("1.cif")


# 如果是一個結構數據庫,讀取後保存爲cif文件
data_file = open("db.json", "r")
all_structures = data_file.readlines()
i = 0
for structure in all_structures:
    structure = json.loads(structure)["structure"]
    structure = Structure.from_dict(structure)
    i = i + 1
    c = CifWriter(structure)
    c.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章