【问题标题】:Writing simultaneously, into several files, the elements of lists of different length同时将不同长度列表的元素写入多个文件
【发布时间】:2017-09-05 18:12:22
【问题描述】:

我有几个列表:

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

我无法写出来自XsYsZsATOMIC_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 项。

【问题讨论】:

  • 您的119.823364.dat121.143469.dat 完全相同。
  • @Arda Arslan 我刚刚编辑了帖子

标签: python list parsing io text-extraction


【解决方案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

【讨论】:

  • 非常感谢您的回答。但是,如问题的第 3 行所述,我正在寻找在列表内容之前和之后使用some stuff 获取119.823364.dat121.143469.dat 文件。我试图做f.write("""some stuff other stuff""" + line + \n + """other stuff some other stuff\n"""),但这不起作用。我怎样才能做到这一点,即像 119.823364.dat file 结果(帖子中的第 3 行)?非常感谢
  • @DavidC.:您可以在添加值之前/之后修改行列表。我会修改我的答案来解决这个问题。
  • 非常感谢您提供的非常有用的帮助
【解决方案2】:

以下将创建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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-09
    • 2018-05-27
    • 2017-06-24
    • 2019-10-25
    • 2021-09-02
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多