[英]Create a numpy array from a line
I have the follwing issue: I read a line using realines, the line is the following: 我有一个下面的问题:我使用realines读了一行,内容如下:
Reference Coordinates: Xref = 0.00000E+000, Yref = -1.20982E-002 参考坐标:Xref = 0.00000E + 000,Yref = -1.20982E-002
i want this line to be converted into a numpy array, so that: 我希望将此行转换为numpy数组,以便:
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. 问题是当即时通讯使用例如np.fromstring(我试过我认为所有类似的numpy例程)时,我无法像这样访问数组。 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 ). 之前也曾提出过类似的问题( Python:从字符串中提取数字 )。 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'] 输出: ['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] 输出: [0。0.00300947]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.