S
S
Sergey30302019-09-18 13:15:06
Python
Sergey3030, 2019-09-18 13:15:06

How to read data from .csv file in numpy skipping non-numeric values?

I have a CSV file

5.8497e-001, 1.4380e-001, -8.1657e-003, 3.9700e+002
5.8468e-001, 1.4616e-001, -8.2995e-003, 3.9782e+002
5.8794e-001, 1.4496e-001, -8.2315e-003, 4.0057e+002
5.8548e-001, 1.5401e-001, -8.7451e-003, 3.9863e+002

[Name]
Nv

[Data]
X [ m ], Y [ m ], Z [ m ], T [ K ]
5.7816e-001, 1.0110e-001, -7.7954e-004, 3.7772e+002
5.7858e-001, 1.0105e-001, -5.7192e-004, 3.7812e+002

How to read it in NumPy skipping lines of text? if I understand correctly, can only skip the first lines. np.loadtxt(path, delimiter=',')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2019-09-18
@Sergey3030

You can use np.genfromtxt(), you can pass it an array of only the necessary lines, throwing out all the unnecessary ones in advance. In the simplest case, you can do this (if you want to use a more accurate check of numbers):

nums = '-0123456789'
# выбираем только цифровые строки
lines = [line for line in csv_file.readlines() if line[0] in nums]
data = np.genfromtxt(lines, delimiter=",")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question