Python Forum
convert 'A B C' to numpy float matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert 'A B C' to numpy float matrix
#1
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
Reply
#2
(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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
and where is your code?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 528 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,594 Jul-19-2022, 06:31 AM
Last Post: paul18fr
Sad ValueError: could not convert string to float badju 0 4,315 Jul-01-2021, 12:13 AM
Last Post: badju
  Indirectlty convert string to float in JSON file WBPYTHON 6 5,936 May-06-2020, 12:09 PM
Last Post: WBPYTHON
  ValueError: could not convert string to float RahulSingh 3 4,172 Apr-09-2020, 02:59 PM
Last Post: dinesh
  How to prepare a NumPy array which include float type array elements subhash 0 1,905 Mar-02-2020, 06:46 AM
Last Post: subhash
  ValueError: could not convert string to float: '4 AVENUE' Kudzo 4 5,916 Jan-26-2020, 10:47 PM
Last Post: Kudzo
  Add column to numpy matrix Gigux 1 4,447 Aug-02-2019, 12:05 AM
Last Post: scidam
  Python/C API Matrix to Numpy jibarra 0 1,871 Jul-29-2019, 03:25 PM
Last Post: jibarra
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 2,974 Apr-09-2019, 04:54 PM
Last Post: PhysChem

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020