Python实现标准.xyz文件导出

P实现内容

指定步数下的xyz文件 xyz文件格式要求

我是为了求RDF和键角分布用到的这个xyz文件,当然应该也会有其他的用处,代码新手所写,不够精炼,各位大佬见谅。

所需文件

轨迹文件,Python

# -*- coding: utf-8 -*-
"""
Created on Thu Apr  2 11:04:29 2020

@author: YuanbaoQiang
"""

initial_step = input('请输入文件的初始步数:')
thermo_step = input('请输入运算步长:')
step_you_need = input('请输入指定步数:')

number=int(step_you_need)/int(thermo_step)

#选取thermo_step*number步数范围的数据
count = []
with open('./optimization.lammpstrj') as f_initial:
    lines = f_initial.readlines()
    number_of_atoms = int(lines[3])
    num = len(lines)
    for i, line in enumerate(lines):
        if line.startswith('ITEM: TIMESTEP'):            
           start = i
           count.append(start)
           
#存储指定步数的header信息()
with open('./data_after_deal_header.txt','w') as f_delete_01:
    for i in range(count[int(number)],count[int(number)]+8):
         f_delete_01.write(lines[i])


with open('./data_after_deal_rest.txt','w') as f_delete_02:
    for i in range(count[int(number)]+9,count[int(number)]+9+number_of_atoms):
         f_delete_02.write(lines[i])         
        
#提取type, x, y, z
f_old = open('./data_after_deal_rest.txt')
line = f_old.readline()
result = []
while line:
    a = line.split()
    b = a[1:2] 
#原子1为C,原子2为H    
    b_after1 = ['C' if x == '1' else x for x in b]
    b_after2 = ['H' if x == '2' else x for x in b_after1]
    for item in b_after2:
        result.append(item)
    for item in a[2:5]:
        result.append(item)        
    line = f_old.readline()
f_old.close()        

with open('./result.xyz','w') as f_new:
    f_new.write(str(int(len(result)/4))+'\n'+'UAPE'+'\n')
    for i in range(len(result)):
        f_new.write(result[i]+' ')
        if (i+1) % 4 == 0:
            f_new.write('\n')
f_new.close()

print('OK,你可以拿去后处理了')

header_file:
在这里插入图片描述
.xyz:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YuanbaoQiang

你的鼓励将是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值