Python Forum

Full Version: convert 'A B C' to numpy float matrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I want to use numpy command to read a matrix which is string format of positions of some atoms, for example, as follows:

['2.8320639688889e+00 8.2431195922222e+00 5.5456174000000e-01'
'2.8490815888889e+00 1.0658281305556e+01 1.9802577466667e+00'
'2.8277318833333e+00 8.2326359600000e+00 1.9737453877778e+00']

and writes converted positions to the float number and merge with atoms symbols (which is another read list containing carbon, hydrogen, and oxygen atoms: C, H, O):

C 2.8320639688889 8.2431195922222 0.55456174000000
H 2.8490815888889 10.658281305556 1.9802577466667
O 2.8277318833333 8.2326359600000 1.9737453877778

Please note that the first format comes from reading by readlines command.

Thanks in advance
(Feb-27-2020, 08:41 AM)rezabma Wrote: [ -> ]Please note that the first format comes from reading by readlines command.

Don't use the readlines() if you don't want to get this result.
Post your code in python tags, sample data file you read from, where the atom symbols come from...
Sorry!
This is my input file:
===============================================================
Output:
output band 0.0 0.0 0.0 0.5 0.5 -0.5 50 Gamma Z output band 0.25 0.25 0.25 0.5 0.5 -0.5 50 P Z pdating Kohn-Sham eigenvalues, eigenvectors, occupation numbers, and Fermi level. Solving generalized eigenproblem Solving hermitian generalised eigenvalue problem by standard ScaLAPACK. Using Cholesky factor for transformation Using 1-stage complex ELPA solver Solving hermitian generalised eigenvalue problem by standard ScaLAPACK. Using Cholesky factor for transformation atom list: C, H, O: 2.8320639688889e+00 8.2431195922222e+00 5.5456174000000e-01 2.8490815888889e+00 1.0658281305556e+01 1.9802577466667e+00 2.8277318833333e+00 8.2326359600000e+00 1.9737453877778e+00
====================================================================

atom list is the 4th line from the end, and atomic positions are three lines from the end. I need to write a file with following data from my input:
===================
Output:
C 2.8320639688889 8.2431195922222 0.55456174000000 H 2.8490815888889 10.658281305556 1.9802577466667 O 2.8277318833333 8.2326359600000 1.9737453877778
========================

Thanks
and where is your code?
Sorry again! I will use tags in future.
About the code, I am a newbie in python. I used following code, but I know I have some errors:

myinput  = open('test.data',mode='r')
myoutput = open('position.xyz',mode='w')

number_atoms=int(input("Please enter number of atoms:"))

data=myinput.readlines()
final=len(data)
#print(final)
start=final-(number_atoms)
#print(start)
atom_list=data[start-1].split()
#print(atom_list[2:6])
coord=data[start:final]
#print(coord)
print(atom_list[2:6],coord,file=myoutput)