0

I have the follwing issue: I read a line using realines, the line is the following:

Reference Coordinates: Xref = 0.00000E+000, Yref = -1.20982E-002

i want this line to be converted into a numpy array, so that:

         array=[nan nan nan nan 0.0 nan nan -1.20982E-002] 

than i want to be able to access this array:

        number1=array[0][4]
        print(number1)
        0.00000E+000
        number2=array[0][6]
        print(number2)
        -1.20982E-002

Problem is when im using e.g. np.fromstring (i tried i think all similar numpy routines) I cannot access the array like this. Either I get:

        Enter file name: ERIS-NIX_106_-FOLDED-Grid_Distortion-cam2-wl3.txt
        ["['Reference" 'Coordinates:' 'Xref' '=' '0.00000E+000,' 'Yref' '='
         "3.00947E-003\\r\\n']"]
       [

       Traceback (most recent call last):
       File "/home/sebo/Documents/Linear Transform For Eris C1/ERIS-NIX_106_-FOLDED-Grid_Distortion(1)/Open_shift.py", line 51, in <module>
      main()
       File "/home/sebo/Documents/Linear Transform For Eris C1/ERIS-NIX_106_-FOLDED-Grid_Distortion(1)/Open_shift.py", line 9, in main
       reader_affine(file_name,d)
       File "/home/sebo/Documents/Linear Transform For Eris C1/ERIS-    NIX_106_-FOLDED-Grid_Distortion(1)/Open_shift.py", line 35, in reader_affine
       print(line[0][i][i])
       IndexError: string index out of range

OR:

      Enter file name: ERIS-NIX_106_-FOLDED-Grid_Distortion-cam2-wl3.txt

      Traceback (most recent call last):
      File "/home/sebo/Documents/Linear Transform For Eris C1/ERIS-                 NIX_106_-FOLDED-Grid_Distortion(1)/Open_shift.py",        line 50, in <module>
      main()
      File "/home/sebo/Documents/Linear Transform For Eris C1/ERIS-           NIX_106_-FOLDED-Grid_Distortion(1)/Open_shift.py", line 9, in main
      reader_affine(file_name,d)
      File "/home/sebo/Documents/Linear Transform For Eris C1/ERIS-NIX_106_-FOLDED-Grid_Distortion(1)/Open_shift.py",               line 29, in reader_affine
      line=np.fromstring(critical_line,dtype=float)
      ValueError: string size must be a multiple of element size

OR I can seperatily access my array but than I get something like this:

      Enter file name: ERIS-NIX_106_-FOLDED-Grid_Distortion-cam2-wl3.txt
      [
      C
      X
      =
      0
      Y
      =
      3

For a loop that goes like this:

        while i in range(0,line.size):
        print(line[0][i])
        i=i+1
Sebastiano1991
  • 867
  • 1
  • 10
  • 26
  • Why does your txt file return double quotations? **"** ['Reference **"** 'Coordinates:' 'Xref' '=' '0.00000E+000,' 'Yref' '=' **"** 3.00947E-003\\r\\n'] **"** – yevgeniy Nov 30 '15 at 16:25

1 Answers1

0

Similar questions have been asked before (Python: Extract numbers from a string). I copied your input list(string) from the question, except removing double quotation marks.

s=['Reference' 'Coordinates:' 'Xref' '=' '0.00000E+000,' 'Yref' '=''3.00947E-003\\r\\n']

Then you can first extract remove the non-number characters, which creates a list of numbers as strings:

 import re
 x=array(re.findall(r"[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?", s[0]))

Output: ['0.00000E+000' '3.00947E-003']

Convert it to float array:

import numpy as np
y = x.astype(np.float)
print y

Output: [ 0. 0.00300947]

Community
  • 1
  • 1
yevgeniy
  • 888
  • 7
  • 14
  • Hello, thanks I stumbled upon this post, but i couldnt get it work before. The issue was that (I assume because of the double quotation marks) the line couldn't be addressed by: `s[0] (print(s[0])>>>[ ])` However applying findall to the whole string worked. Thanks, and sorry for the double posting. – Sebastiano1991 Dec 01 '15 at 09:04