Python3:将信息从文本文件写入csv文件

时间:2013-09-24 10:17:34

标签: python csv python-3.x os.walk

我有多个目录,每个目录都包含多个文件:

Ce  
+---top.txt
+---X0.0        
|     |  
|     +---Y0.0  
|     |     |
|     |     +---X0.0Y0.0Z0.0.dat
|     |     +---X0.0Y0.0Z0.0.out
|     |     +---X0.0Y0.0Z0.05.dat
|     |     +---X0.0Y0.0Z0.05.out   
|     +---Y0.05
|           |
|           +---X0.0Y0.05Z0.0.dat
|     |     +---X0.0Y0.0Z0.05.out
|           +---X0.0Y0.05Z0.05.dat
|           +---X0.0Y0.05Z0.05.out
+---X0.05
      |  
      +---Y0.0  
      |     |
      |     +---X0.0Y0.0Z0.0.dat
      |     +---X0.0Y0.0Z0.0.out
      |     +---X0.0Y0.0Z0.05.dat
      |     +---X0.0Y0.0Z0.05.out   
      +---Y0.05
            |
            +---X0.0Y0.05Z0.0.dat
            +---X0.0Y0.05Z0.0.out
            +---X0.0Y0.05Z0.05.dat
            +---X0.0Y0.05Z0.05.out

我试图从'.out'文件中提取相关信息并将其写入csv文件。为此,我设计了以下代码:

import os
import csv

with open('results.csv', 'a', newline='') as f:
    writer=csv.writer(f)
    writer.writerow(['Filename', 'Optimisation Achieved?', 'Final Energy', 'Number of Defects', 'Defect Charge', 'M-L Centre X', 'M-L Centre Y', 'M-L Centre Z', 'Defect X', 'Defect Y', 'Defect Z', 'Defect Energy']) 
    for root, dirs, files in os.walk('.'):
        for filename in files:
            if filename.endswith('.out'):
                file = os.path.join(root, filename)
                with open(file, 'r') as reader:
                    opt_cnt=0
                    for line in reader:
                        s=line.strip()
                        if s=='**** Optimisation achieved ****':
                            opt_cnt+=1
                            if opt_cnt==1:
                                opt1='Y' + str(opt_cnt)
                            if opt_cnt==2:
                                opt2='Y' + str(opt_cnt)
                            else:
                                opt2='N'
                        elif s.startswith('Final energy='):
                            Final=s[18:31]
                        elif s.startswith('Total Number of Defects'):
                            Number=s[31]
                        elif s.startswith('Total charge on defect'):
                            Q=s[-4:]
                        elif s.startswith('Defect centre'):
                            centrex=s[21:28]
                            centrey=s[31:37]
                            centrez=s[40:46]
                        elif s.startswith('Atom/Fractional'):
                            coordx=s[40:49]
                            coordy=s[51:60]
                            coordz=s[62:71]
                        elif s.startswith('Final defect energy'):
                            Defect_Energy=s[-13:]
                writer.writerow([filename, opt1, Final, Number, centrex, centrey, centrez, coordx, coordy, coordz, Defect_Energy])

但是,这会产生以下错误:

追踪(最近一次通话):
  文件“C:\ Users \ Rebecca \ Dropbox \ Brannerite \ Published Potentials \ interstitials \ Grid \ Ce \ results.py”,第39行,中
    writer.writerow([filename,opt1,Final,Number,centrex,centrey,centrez,coordx,coordy,coordz,Defect_Energy])
NameError:名称'Final'未定义

[改变我的代码中最后一行的缩进量会改变nameError中出现的变量:因为代码有16个缩进(同样是NameError有20个缩进);将此更改为8,12或24会给出' NameError:名称'opt1'未定义]

我应该说大部分代码[删除与os.walk相关的行]已经成功用于一组不同的文件(都在一个目录中)。

有人可以在上面的代码中提出错误的来源以及如何纠正错误吗?

2 个答案:

答案 0 :(得分:1)

如果out文件没有以Final energy=开头的行,则Final保持未定义。您可能希望为循环中定义的FinalNumber等变量定义默认值,例如将它们全部设置为undefined string:

Final = Number = ... = 'undefined'
file = os.path.join(root, filename)
with open(file, 'r') as reader:
    opt_cnt=0
    for line in reader:
        ...

答案 1 :(得分:0)

我认为您希望有2个Vars Final和opt1定义。您的代码中不是这种情况

writer.writerow([filename, opt1, Final, Number, centrex, centrey, centrez, coordx, coordy,    coordz, Defect_Energy])

由于if / elif构造,您的代码只允许定义其中的1个或opt1或Final:

if s=='**** Optimisation achieved ****':
    opt_cnt+=1
    if opt_cnt==1:
        opt1='Y' + str(opt_cnt)
elif s.startswith('Final energy='):
    Final=s[18:31]

也许尝试预先定义默认值或更改应用的逻辑

相关问题