简体   繁体   中英

Create a numpy array from a line

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 eg 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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM