修改数据文件:savetxt标题选项

时间:2016-12-08 15:08:41

标签: python numpy

我有一堆来自LAMMPS模拟的文件,由9行标题和Nx5数据数组(N很大,订单10000)组成。文件如下所示:

ITEM: TIMESTEP
1700000
ITEM: NUMBER OF ATOMS
40900
ITEM: BOX BOUNDS pp pp pp
0 59.39
0 59.39
0 59.39
ITEM: ATOMS id type xu yu zu 
1 1 -68.737755560980844 1.190046376093027 122.754819323806714 
2 1 -68.334493269859621 0.365731265115530 122.943111038981527 
3 1 -68.413018326512173 -0.456802254452782 123.436843456292138 
4 1 -68.821350328206080 -1.360098170077123 123.314784135612115 
5 1 -67.876948635447775 -1.533699833382506 123.072964235308660 
6 1 -67.062910322675322 -2.006415676993953 123.431518511867381 
7 1 -67.069984116148134 -2.899068427170739 123.057125785834685 
8 1 -66.207325578729183 -3.292545155979909 123.377770523297343 
...

我想打开每个文件,对数字数据执行某个操作,并使用不同的名称保存文件,使标题保持不变。我的剧本是:

for f in files:
filename=path+"/"+f
with open(filename) as myfile:
        header = ' '.join([next(myfile) for x in xrange(9)]) 
data=np.loadtxt(filename,skiprows=9)
data[:,2:5]%=L #Put everything inside the box...
savetxt(filename.replace("lammpstrj","fold.lammpstrj"),data,header=header,comments="",fmt="%d %d %.15f %.15f %.15f")

但是输出看起来像这样:

ITEM: TIMESTEP
 1700000
 ITEM: NUMBER OF ATOMS
 40900
 ITEM: BOX BOUNDS pp pp pp
 0 59.39
 0 59.39
 0 59.39
 ITEM: ATOMS id type xu yu zu 

1 1 50.042244439019157 1.190046376093027 3.974819323806713
2 1 50.445506730140380 0.365731265115530 4.163111038981526
3 1 50.366981673487828 58.933197745547218 4.656843456292137
4 1 49.958649671793921 58.029901829922878 4.534784135612114
5 1 50.903051364552226 57.856300166617494 4.292964235308659
6 1 51.717089677324680 57.383584323006048 4.651518511867380
7 1 51.710015883851867 56.490931572829261 4.277125785834684
8 1 52.572674421270818 56.097454844020092 4.597770523297342
...

标题不完全相同:除了第一行之外,每行的开头都有空格,标题的最后一行之后有一个换行符。我需要摆脱那些,但我不知道如何。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

问题出在' '.join(a)

a = ['sadf\n', 'sdfg\n']
' '.join(a)
 >>>'sadf\n sdfg\n' # Note the space at the start of the second line.

相反:

''.join(a)
>>>'sadf\nsdfg\n'

您还需要修剪标题中的最后一个'\ n'以防止空行:

''.join(a).rstrip()
>>>'sadf\nsdfg'

答案 1 :(得分:0)

header参数会自动添加换行符,因此您可以删除原始的最后一行' \ n'作为一个冗余的换行符。

header = header.rstrip('\n')

出现前导空格,因为您通过额外的空格字符连接每一行。你可以通过以下命令解决它。

header = ''.join([next(myfile) for x in xrange(9)])