我有一系列 .src 文件,我试图使用 DictReader() 将它们输入到字典中。文件如下所示(只是标题和第一行):
SRC V2.0.. ........Time Id Event T Conf .Northing ..Easting ...Depth Velocity .NN_Err .EE_Err .DD_Err .NE_Err .ND_Err .ED_Err Ns Nu uSt ....uMag Nt tSt ....tMag .MomMag SeiMoment ...Energy ...Es/Ep .SourceRo AspRadius .StaticSD AppStress DyStressD MaxDispla PeakVelPa PeakAccPa PSt
07-30-2010 07:43:56.543 ND 0 e 0.00 152.54 746.45 1686.31 6000 11.76 11.76 11.76 0.00 0.00 0.00 30 0 num -9.90 30 utm -3.21 -1.12 2.06e+007 2.22e+000 20.93 6.08e+000 0.00e+000 3.83e+004 1.49e+003 0.00e+000 1.52e-005 1.50e-003 0.00e+000 1
无论如何,以下是我的代码:
import csv
Time = {}
Northing = {}
source_file = open(NNSRC, 'rb')
for line in csv.DictReader(source_file, delimiter = '\t'):
Time = line['........Time'].strip()
Northing = line['.Northing'].strip()
print Time, Northing
它给了我以下错误:
Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\xy\NNFindStages.py", line 101, in <module>
Time = line['........Time'].strip()
KeyError: '........Time'
如何在不更改文件本身的情况下解释文件头在文件中格式化的奇怪方式?
任何帮助是极大的赞赏!