2017-09-05 92 views
0

名單的元素我有幾個列表:同時寫入,分成幾個文件,不同長度

VOLUMES = ['119.823364', '121.143469'] 
P0 = ['4.97568007', '4.98494429'] 
P2 = ['16.76591397', '16.88768068'] 
Xs = ['0.000000000000E+00', '3.333333333333E-01', '-4.090760942850E-01', '0.000000000000E+00', '3.333333333333E-01', '-4.093755657782E-01'] 
Ys = ['0.000000000000E+00', '-3.333333333333E-01', '-3.333333333333E-01', '0.000000000000E+00', '-3.333333333333E-01', '-3.333333333333E-01'] 
Zs = ['0.000000000000E+00', '-8.333333333333E-02', '-8.333333333333E-02', '0.000000000000E+00', '-8.333333333333E-02', '-8.333333333333E-02'] 
ATOMIC_NUMBERS = ['20', '6', '8', '20', '6', '8'] 

而且我想生成2個文件,用VOLUMES列表的項目命名爲:119.823364.dat121.143469.dat ,如每一個包括以下內容:

119.823364.dat文件:

some stuff 
other stuff 
4.97568007 16.76591397 
3 
20 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00 
6 3.333333333333E-01 -3.333333333333E-01 -8.333333333333E-02 
8 -4.090760942850E-01 -3.333333333333E-01 -8.333333333333E-02 
other stuff 
some other stuff 

121.143469.dat文件:

some stuff 
other stuff 
4.98494429 16.88768068 
3 
20 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00 
6 3.333333333333E-01 -3.333333333333E-01 -8.333333333333E-02 
8 -4.093755657782E-01 -3.333333333333E-01 -8.333333333333E-02 
other stuff 
some other stuff 

存在以下問題:

len(VOLUMES) = len(P0) = len(P2) = 2

但是:

len(Xs) = len(Ys) = len(Zs) = 6

我已成功地實現了第一部分:

# Remove *.dat files, to clean first: 
for f in glob.glob("*.dat"): 
    os.remove(f) 

# Create the files: 
filenames = [] 
for V in VOLUMES: 
    filename = "{}.dat".format(V) 
    print 'filename = ', filename 
    filenames.append(filename) 
print filenames 

# Write to files: 
for i in xrange(len(P0)): 
     with open(filenames[i],'w') as f: 
     f.write("""some stuff 
other stuff\n""") 
     f.write("{} {}\n".format(P0[i], P2[i])) 
     f.write("{}\n".format(N_atom_irreducible_unit)) 

它創建如下:

119.823364.dat文件:

some stuff 
other stuff 
4.97568007 16.76591397 
3 

121.143469.dat文件:

some stuff 
other stuff 
4.98494429 16.88768068 
3 

我不能設法從Xs寫信息,YsZsATOMIC_NUMBERS因爲這些4列表的長度不同於P0P2

我設法重新寫XsYsZsATOMIC_NUMBERS成一個單一的list of list of lists

for index_vol in range(len(VOLUMES)): 
    for index in range(len(ATOMIC_NUMBERS)): 
    atoms_per_frame = [ATOMIC_NUMBERS[index], Xs[index], Ys[index], Zs[index]] 
    atoms_all_frames[index_vol].append(atoms_per_frame) 

print atoms_all_frames 

其打印如下:

[[['20', '0.000000000000E+00', '0.000000000000E+00', '0.000000000000E+00'], ['6', '3.333333333333E-01', '-3.333333333333E-01', '-8.333333333333E-02'], ['8', '-4.090760942850E-01', '-3.333333333333E-01', '-8.333333333333E-02'], ['20', '0.000000000000E+00', '0.000000000000E+00', '0.000000000000E+00'], ['6', '3.333333333333E-01', '-3.333333333333E-01', '-8.333333333333E-02'], ['8', '-4.093755657782E-01', '-3.333333333333E-01', '-8.333333333333E-02']], [['20', '0.000000000000E+00', '0.000000000000E+00', '0.000000000000E+00'], ['6', '3.333333333333E-01', '-3.333333333333E-01', '-8.333333333333E-02'], ['8', '-4.090760942850E-01', '-3.333333333333E-01', '-8.333333333333E-02'], ['20', '0.000000000000E+00', '0.000000000000E+00', '0.000000000000E+00'], ['6', '3.333333333333E-01', '-3.333333333333E-01', '-8.333333333333E-02'], ['8', '-4.093755657782E-01', '-3.333333333333E-01', '-8.333333333333E-02']]] 

我不知道是否創建該list of list of lists是能夠與for i in xrange(len(P0)):一起循環的解決方案,但我無法完成此任務。

實際上,VOLUMES列表的長度約爲50個項目。

+0

你的'119.823364.dat'和'121.143469.dat'是一樣的。 –

+0

@Arda Arslan我剛剛編輯帖子 –

回答

1

使用zip建立自己的線,並將結果分成len(rows)/len(volumes)尺度的街區。然後,將每個塊寫入其各自的文件。

headers = list(zip(P0, P2)) 
rows = [row for row in zip(ATOMIC_NUMBERS, Xs, Ys, Zs)] 
interval = int(len(rows)/len(VOLUMES)) 

for block_i, vol_i in zip(range(0, len(rows), interval), range(len(VOLUMES))): 
    # Create the lines for the file 
    lines = [' '.join(headers[vol_i]), '3'] 
    lines += [' '.join(row) for row in rows[block_i : block_i + interval]] 
    # Write the file 
    with open(VOLUMES[vol_i] + '.dat', 'w') as f: 
     # Preceding lines 
     f.write('some stuff\nother stuff') 
     # Lines of data 
     for line in lines: 
      f.write(line + '\n') 
     # Trailing lines 
     f.write('other stuff\nsome other stuff') 

文件119.823364.dat將包含:

some stuff 
some other stuff 
4.97568007 16.76591397 
3 
20 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00 
6 3.333333333333E-01 -3.333333333333E-01 -8.333333333333E-02 
8 -4.090760942850E-01 -3.333333333333E-01 -8.333333333333E-02 
other stuff 
some other stuff 

注意,這種方法是動態的,將任意長度的VOLUMES工作。

+0

非常感謝您的回答。但是,正如問題的第三行文字所述,我一直在尋找在列表內容前後使用'some stuff'獲取'119.823364.dat'和'121.143469.dat'文件。我試圖做'f.write(「」「一些東西 其他東西」「」+ line + \ n +「」「其他東西 其他東西\ n」「」)''但是這不起作用。我怎麼能做到這一點,就像'119.823364.dat文件'結果(帖子中的第三行)?非常感謝 –

+0

@DavidC .:您可以在添加值之前/之後修改行列表。我會修改我的答案來解決這個問題。 –

+0

非常感謝您的幫助 –

0

下面創建119.823364.dat

VOLUMES = ['119.823364', '121.143469'] 
P0 = ['4.97568007', '4.98494429'] 
P2 = ['16.76591397', '16.88768068'] 
Xs = ['0.000000000000E+00', '3.333333333333E-01', '-4.090760942850E-01', 
     '0.000000000000E+00', '3.333333333333E-01', '-4.093755657782E-01'] 
Ys = ['0.000000000000E+00', '-3.333333333333E-01', '-3.333333333333E-01', 
     '0.000000000000E+00', '-3.333333333333E-01', '-3.333333333333E-01'] 
Zs = ['0.000000000000E+00', '-8.333333333333E-02', '-8.333333333333E-02', 
     '0.000000000000E+00', '-8.333333333333E-02', '-8.333333333333E-02'] 
ATOMIC_NUMBERS = ['20', '6', '8', '20', '6', '8'] 

with open('{}.dat'.format(VOLUMES[0]), 'w+') as file: 
    file.write('some stuff \n') 
    file.write('other stuff \n') 
    for item in P0: 
     file.write('{} '.format(item)) 
    file.write('\n') 
    file.write('3 \n') 
    for i in range(3): 
     file.write('{} {} {} {} \n'.format(ATOMIC_NUMBERS[i], 
              Xs[i], Ys[i], Zs[i])) 
    file.write('other stuff') 

你會遵循類似的過程,用於創建121.143469.dat