yodama
yodama

Reputation: 809

Python: Removing non-numeric data from CSV

I am trying to read CSV files generated by some code I've written on my raspberry pi. Currently, the raspberry pi outputs data with a few lines of warnings or data in the first lines, and then outputs the data stream as numerical values like so:

MMA init error = -82        
MMA init error = 0      
MMA init pass    ID = 26    
MMA Sensor Connected    4744    56100
65232   4744    56100
65232   4744    56100
65232   4744    56100
65232   4744    56100

I want a way to be able to parse through this and remove all lines with errors and information, so that when I'm running analytics on the numerical data, the other data won't be included. Is there a way to do this, similar to how in MATLAB, you can simply write filename.data?

Upvotes: 1

Views: 939

Answers (1)

crowdedComputeeer
crowdedComputeeer

Reputation: 469

# coding: utf-8

data =[]
with open(filename) as f:
    for line in f.readlines():
        fields = line.split('\t')
        if fields[0].isdigit():
            data.append(fields)

or use pandas

from pandas import read_table
# if you know first row data shows up in 
data = read_table(filename, header=firstrowdata)

Upvotes: 1

Related Questions