使用Python删除文件的第一列,同时删除头节

2024-10-02 00:20:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正试图写一个程序来删除前15行的文本和第一列的空格分隔的文件,但到目前为止,我还没有成功,有人知道我哪里出错了吗?你知道吗

输入文件如下所示

(Version 6f format configuration file) Metadata title: gamma_glycine_30C Number of moves generated: 1 Number of moves tried: 0 Number of moves accepted: 0 Number of prior configuration saves: 0 Number of atoms: 30000 Number density (Ang^-3): 0.000000 Supercell dimensions: 10 10 10 Cell (Ang/deg): 70.300823 70.300822 54.806050 90.000000 90.000000 120.000001 Lattice vectors (Ang): 60.882298 -35.150412 0.000000 0.000000 70.300822 0.000000 0.000000 0.000000 54.806050 Atoms: 1 C 0.000124 0.039337 0.089331 2 C 0.060663 0.060787 0.022665 3 C 0.039213 0.099876 0.055998 4 C 0.002607 0.042231 0.062178 5 C 0.057769 0.060376 0.095512

目前的代码如下所示:

# This line allows the user to input the particular filename
filename = input( "Filename (excluding .rmc6f):")
molecules = int(input( "No. of molecules in rmc6f :"))
atoms = int(input( "No. of atoms in rmc6f :"))

#These lines create the .xyz file heading that we will append to later
xyz = open(filename+'.xyz' , 'w')
xyz.write( str(atoms) + '\n' )
xyz.write( str(filename) + '\n' )

#these lines create a stripped down version of the .rmc6f file with only the important columns
import csv
import re
from itertools import islice

row_split = re.compile('\s*\*\s*')

with open(filename+'.rmc6f', 'r') as infile, open(filename+'.intermediate', 'w', newline='') as outfile:
    #rdr= csv.reader(filename+'.rmc6f', delimiter='\t')
    writer = csv.writer(outfile, delimiter='\t')

    next(islice(infile, 15, 15), None) # skip the first 15 lines in the input file

    for r in infile:
        writer.writerow((r[1], r[2], r[3], r[4]))

目前输出的只是.xyz文件标题str(atoms)和str(filename),没有坐标,有人能告诉我哪里出错了吗?你知道吗


Tags: 文件oftheinnumberinputfilenamefile

热门问题