'Why do I have a KeyError, and how can I fix this (Python)?
I am trying to extract some data from a .dat file so I can graph it using Python. However, I am experiencing a KeyError. This is the code that I have written.
fig, axs = plt.subplots(1, nno, figsize=(19,12), sharey=True)
for i in np.arange(nno):
atoms = ase.io.read(objs[i].scffile(), format='espresso-in')
full_attach_charges(atoms, objs[i].path+'/XAS/Zn0.opt.in.out/GS/'+'ACF.dat',displacement=None, valence=True)
charges = []
for atom in atoms:
charges.append((atom.symbol, atom.charge))
#atom number here starting from 1 (and not pythonic 0)
axs[i].scatter(atom.index+1, atom.charge, c=colors[atom.symbol],edgecolors='black')
markers = [plt.Line2D([0,0],[0,0],color=color, marker='o', linestyle='') for color in colors.values()]
axs[i].legend(markers, colors.keys(), numpoints=1)
axs[i].set_xlabel('Atom number')
axs[i].set_title(letters[i] + ' ' + names[i]+ ': '+ names[i], loc='left')
axs[0].set_ylabel('Bader charge')
print(atoms.info[atom.symbol]['valence'])
#Save plot
plt.savefig('bader.pdf')
This is the error I am getting.
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-23-7ac23d8418f1> in <module>
6 for i in np.arange(nno):
7 atoms = ase.io.read(objs[i].scffile(), format='espresso-in')
----> 8 full_attach_charges(atoms, objs[i].path+'/XAS/Zn0.opt.in.out/GS/'+'ACF.dat',displacement=None, valence=True)
9 charges = []
10
<ipython-input-4-574dffc1a6e5> in full_attach_charges(atoms, fileobj, displacement, valence)
62 atom = atoms[int(words[0]) - 1]
63 if valence is True:
---> 64 atom.charge = atoms.info[atom.symbol]['valence'] - float(words[j])
65 else:
66 atom.charge = atomic_numbers[atom.symbol] - float(words[j])
KeyError: 'H'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
