同时写入几个文件,不同长度的列表元素 [英] Writing simultaneously, into several files, the elements of lists of different length

查看:180
本文介绍了同时写入几个文件,不同长度的列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个清单:

  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.dat 121.143469.dat ,例如每个con以下是:



119.823364.dat file:

 一些东西
其他东西
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
其他东西
其他一些东西

121.143469.dat file:

 一些东西
其他东西
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
其他东西
其他一些东西

存在以下问题:



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



<但是:



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



我已成功实现第一部分:

 #删除* .dat文件,首先清理:
表示glob.glob中的f(*。dat):
os.remove(f)

#创建文件:
filenames = []
for V in VOLUMES:
filename ={}。 dat.format(V)
print'filename =',filename
filenames.append(filename)
print filenames

#写入文件:
for x 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 file:

 一些东西
其他东西
4.97568007 16.76591397
3

121.143469.dat fi le:

 一些东西
其他东西
4.98494429 16.88768068
3

我无法从 Xs ,<$ c写入信息$ c> Ys , Zs ATOMIC_NUMBERS 因为这4个列表的长度不同于 P0 P2



我设法重新写 Xs Ys Zs ATOMIC_NUMBERS 到单个列表列表中

 表示范围内的index_vol(len(VOLUMES)):
表示范围内的索引(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.33333333333E-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.3333333333333E-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']]] 

我不知道是否创建此列表列表是能够在xrange(len(P0))中与一起循环的解决方案:但我无法完成此任务。



实际上, VOLUMES 列表的长度约为50件。

解决方案

使用 zip 构建行,并将结果拆分为 len (行)/ len(卷) - 大小的块。然后,将每个块写入其各自的文件。

  headers = list(zip(P0,P2))
行= [zip行中的行(ATOMIC_NUMBERS,Xs,Ys,Zs)]
interval = int(len(rows)/ len(VOLUMES))

为block_i,vol_i为zip( range(0,len(rows),interval),range(len(VOLUMES))):
#为文件
lines = [''。join(headers [vol_i])创建行, '3']
行+ = [''。join(行)行中的行[block_i:block_i + interval]]
#用open打开文件
(VOLUMES [vol_i] +'。dat','w')为f:
#前面的行
f.write('some stuff\\\
other stuff')
#数据行
for行中的行:
f.write(行+'\ n')
#尾随行
f.write('其他东西'其他东西')

文件 119.823364.dat 将包含:

 一些东西
其他一些东西
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
其他东西
其他一些东西

请注意,此方法是动态的,适用于任何长度的 VOLUMES


I have several lists:

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']

And I would like to generate 2 files, named with the item of the VOLUMES list: 119.823364.dat and 121.143469.dat, such as each one contain the following:

119.823364.dat file:

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 file:

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

There is the following issue:

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

But:

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

I have managed to achieve the first part:

# 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))

Which creates the following:

119.823364.dat file:

some stuff
other stuff
4.97568007 16.76591397
3

121.143469.dat file:

some stuff
other stuff
4.98494429 16.88768068
3

I cannot manage to write the information from Xs, Ys, Zs and ATOMIC_NUMBERS because these 4 lists have different length than both P0 and P2.

I managed to re-write Xs, Ys, Zs and ATOMIC_NUMBERS into one single 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  

which prints the following:

[[['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']]]

I do not know if creating this list of list of lists is the solution for being able to loop together with for i in xrange(len(P0)): but I cannot manage to accomplish this.

In practice, the VOLUMES list will be of length around ~ 50 items.

解决方案

Use zip to build your lines, and split the result into len(rows) / len(volumes)-sized blocks. Then, write each block to its respective file.

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')

File 119.823364.dat will contain:

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

Note that this approach is dynamic and will work for a VOLUMES of any length.

这篇关于同时写入几个文件,不同长度的列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆