I have a csv file that looks like this:
TEST
2012-05-01 00:00:00.203 ON 1
2012-05-01 00:00:11.203 OFF 0
2012-05-01 00:00:22.203 ON 1
2012-05-01 00:00:33.203 OFF 0
2012-05-01 00:00:44.203 OFF 0
TEST
2012-05-02 00:00:00.203 OFF 0
2012-05-02 00:00:11.203 OFF 0
2012-05-02 00:00:22.203 OFF 0
2012-05-02 00:00:33.203 OFF 0
2012-05-02 00:00:44.203 ON 1
2012-05-02 00:00:55.203 OFF 0
and cannot get rid of the "TEST"
string.
Is it possible to check whether a line starts with a date and read only those that do?
line_number = 8 # the row you want. 0-indexed import pandas as pd import sys # or `import itertools` import csv # you can wrap this block in a function: # (filename, line_number[, max_rows]) -> row with open(filename, 'r') as f: r = csv. reader(f) for i in range(sys.
In the Pandas DataFrame we can find the specified row value with the using function iloc(). In this function we pass the row number as parameter.
from cStringIO import StringIO
import pandas
s = StringIO()
with open('file.csv') as f:
for line in f:
if not line.startswith('TEST'):
s.write(line)
s.seek(0) # "rewind" to the beginning of the StringIO object
pandas.read_csv(s) # with further parameters…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With