user247534
user247534

Reputation: 123

What is the best way to read columnar data from .txt file which includes complex numbers?

I have a text file of the form shown in attached image.5-column data. Colum 0,1: Position coord x,y. Column 3,4,5: Complex fields

with almost 200 data points.

How to read them efficiently in form of arrays such that a=array([2,3,...]); d=array([4+5j,3+1j,....])?

Upvotes: 2

Views: 182

Answers (2)

hpaulj
hpaulj

Reputation: 231385

Looks like your text is formatted in a way that genfromtxt can handle it without problems.

Ignoring the column names and such for now, a sample line would be:

In [235]: txt = """-1.99 -1.99 1.07+0.165j"""

Specify dtype to be float and complex for the appropriate columns:

In [240]: data = np.genfromtxt([txt,txt],dtype=(float,float,complex))
In [241]: data
Out[241]: 
array([(-1.99, -1.99, 1.07+0.165j), (-1.99, -1.99, 1.07+0.165j)],
      dtype=[('f0', '<f8'), ('f1', '<f8'), ('f2', '<c16')])

The result is a structured array (1d). Fields are accessed by name:

In [242]: data['f0']
Out[242]: array([-1.99, -1.99])
In [243]: data['f2']
Out[243]: array([1.07+0.165j, 1.07+0.165j])

The pandas csv reader is faster, but with only 200 data points, I don't think speed will be much of an issue.

Upvotes: 2

Bob Baxley
Bob Baxley

Reputation: 3751

Use pandas.

If the space between columns is a tab, then sep='\t', otherwise use the solution from Can pandas handle variable-length whitespace as column delimiters

Assuming it is a tab, the code to extract the third column of complex numbers is something like

import pandas as pd

df = pd.read_csv('test.txt',sep='\t',skiprows=3)
df.columns = [f'col_{i}' for i in range(len(df.columns))]

column_2_complex_values = df.col_2.map(complex).values # this is a numpy array

Upvotes: 0

Related Questions