Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 268 characters in body
Source Link
Danica
  • 29k
  • 6
  • 94
  • 128

Here's a more direct way than @Jeff's answer, telling loadtxt to load it in straight to a complex array, using a helper function parse_pair that maps (1.2,0.16) to 1.20+0.16j:

>>> import re >>> import numpy as np >>> pair = re.compile(r'\(([^,\)]+),([^,\)]+)\)') >>> def parse_pair(s): ... return complex(*map(float, pair.match(s).groups())) >>> s = '''1 (1.2,0.16) (2.8,1.1) 2 (2.85,6.9) (5.8,2.2)''' >>> from cStringIO import StringIO >>> f = StringIO(s) >>> np.loadtxt(f, delimiter=' ', dtype=np.complex, ... converters={1: parse_pair, 2: parse_pair}) array([[ 1.00+0.j , 1.20+0.16j, 2.80+1.1j ], [ 2.00+0.j , 2.85+6.9j , 5.80+2.2j ]]) 

Or in pandas:

>>> import pandas as pd >>> f.seek(0) >>> pd.read_csv(f, delimiter=' ', index_col=0, names=['a', 'b'], ... converters={1: parse_pair, 2: parse_pair}) a b 1 (1.2+0.16j) (2.8+1.1j) 2 (2.85+6.9j) (5.8+2.2j) 

Here's a more direct way than @Jeff's answer, telling loadtxt to load it in straight to a complex array, using a helper function parse_pair that maps (1.2,0.16) to 1.20+0.16j:

>>> import re >>> import numpy as np >>> pair = re.compile(r'\(([^,\)]+),([^,\)]+)\)') >>> def parse_pair(s): ... return complex(*map(float, pair.match(s).groups())) >>> s = '''1 (1.2,0.16) (2.8,1.1) 2 (2.85,6.9) (5.8,2.2)''' >>> from cStringIO import StringIO >>> f = StringIO(s) >>> np.loadtxt(f, delimiter=' ', dtype=np.complex, ... converters={1: parse_pair, 2: parse_pair}) array([[ 1.00+0.j , 1.20+0.16j, 2.80+1.1j ], [ 2.00+0.j , 2.85+6.9j , 5.80+2.2j ]]) 

Here's a more direct way than @Jeff's answer, telling loadtxt to load it in straight to a complex array, using a helper function parse_pair that maps (1.2,0.16) to 1.20+0.16j:

>>> import re >>> import numpy as np >>> pair = re.compile(r'\(([^,\)]+),([^,\)]+)\)') >>> def parse_pair(s): ... return complex(*map(float, pair.match(s).groups())) >>> s = '''1 (1.2,0.16) (2.8,1.1) 2 (2.85,6.9) (5.8,2.2)''' >>> from cStringIO import StringIO >>> f = StringIO(s) >>> np.loadtxt(f, delimiter=' ', dtype=np.complex, ... converters={1: parse_pair, 2: parse_pair}) array([[ 1.00+0.j , 1.20+0.16j, 2.80+1.1j ], [ 2.00+0.j , 2.85+6.9j , 5.80+2.2j ]]) 

Or in pandas:

>>> import pandas as pd >>> f.seek(0) >>> pd.read_csv(f, delimiter=' ', index_col=0, names=['a', 'b'], ... converters={1: parse_pair, 2: parse_pair}) a b 1 (1.2+0.16j) (2.8+1.1j) 2 (2.85+6.9j) (5.8+2.2j) 
Source Link
Danica
  • 29k
  • 6
  • 94
  • 128

Here's a more direct way than @Jeff's answer, telling loadtxt to load it in straight to a complex array, using a helper function parse_pair that maps (1.2,0.16) to 1.20+0.16j:

>>> import re >>> import numpy as np >>> pair = re.compile(r'\(([^,\)]+),([^,\)]+)\)') >>> def parse_pair(s): ... return complex(*map(float, pair.match(s).groups())) >>> s = '''1 (1.2,0.16) (2.8,1.1) 2 (2.85,6.9) (5.8,2.2)''' >>> from cStringIO import StringIO >>> f = StringIO(s) >>> np.loadtxt(f, delimiter=' ', dtype=np.complex, ... converters={1: parse_pair, 2: parse_pair}) array([[ 1.00+0.j , 1.20+0.16j, 2.80+1.1j ], [ 2.00+0.j , 2.85+6.9j , 5.80+2.2j ]])