有条件地在一行中写2个站点

时间:2014-09-22 10:29:04

标签: python-3.x

我正在尝试解析看起来像的输入:

 1.  Ni  type=1   np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ni1.pot
 2.  Ni  type=2   np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ni2.pot
 3.  Ni  type=3   np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ni3.pot
 4.  Ni  type=4   np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ni4.pot
 5.  Mn  type=5   np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn1.pot
 6.  Mn  type=6   np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn2.pot
 7.  Mn  type=7   np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn3.pot
 8.  Mn  type=8   np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn4.pot
 9.  Mn  type=9   np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn5.pot
 10. Mn  type=10  np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn6.pot
 11. Mn  type=11  np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn7.pot
 12. Mn  type=12  np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn8.pot
 13. Ge  type=13  np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ge1.pot
 14. Ge  type=14  np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ge2.pot
 15. Ge  type=15  np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ge3.pot
 16. Ge  type=16  np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ge4.pot
-------------------------------------------------------------------------------
------------------------------------- CPA -------------------------------------
-------------------------------------------------------------------------------
 1.  cpasite=5 nsubl=2 cpatypes=5,6
 2.  cpasite=6 nsubl=2 cpatypes=7,8
 3.  cpasite=7 nsubl=2 cpatypes=9,10
 4.  cpasite=8 nsubl=2 cpatypes=11,12

预期输出应该是:

Atoms=<pfile for Type1>
Atoms=<pfile for Type2>
.....
Atoms=<pfile for type5> <pfile for type6>
等等。 即如果类型== cpatypes [1],(如cpatypes中的5 = 5,6),则type6将写在同一行。对于这个特定的例子,正确的输出是:

  atoms  = Ni.pot
  atoms  = Ni.pot
  atoms  = Ni.pot
  atoms  = Ni.pot
  atoms  = Mn.pot Mn.pot
  atoms  = Mn.pot Mn.pot
  atoms  = Mn.pot Mn.pot
  atoms  = Mn.pot Mn.pot
  atoms  = Ge.pot
  atoms  = Ge.pot
  atoms  = Ge.pot
  atoms  = Ge.pot

我现在的代码正在拾取所有元素并以新的方式编写。我想我必须创建一个新的循环来检查cpatypes [0] == i,但我无法实现这一点。 以下是我迄今取得的成就:

#!/usr/bin/python3
import re

f1 = open('file.str', 'r')
pattern3 = r'(\d+)\.\s*(.*)\s+ type=(\d+).* pfile=(.*)'
pattern4 = r'(\d+)\. \s* cpasite=(.*)\s* nsubl=(.*)\s* cpatypes=(.*)'

ocount = []
atype = []
apots = []
ofiles = []
xx = []
ckomp = []
sites = []
xx2 = []

for line in f1:
    match3 = re.search(pattern3, line)
    match4 = re.search(pattern4, line)
    if match3:
        ocount.append(int(match3.group(1)))
        atype.append((match3.group(2)))
        apots.append((match3.group(3)))
        ofiles.append(match3.group(4))
    if match4:
        xx.append(match4.group(1))
        xx2.append(match4.group(2))
        ckomp.append(match4.group(3))
        sites.append(match4.group(4))
f1.close()
print(sites)
with open("trialof", "w") as out:
    out.write("Sublattice\n")
    for cnt in apots:
        i = apots.index(cnt)
        out.write("  atoms  = " + atype[i] + ".pot\n")

请帮助。

0 个答案:

没有答案