Вопрос по python – Можно ли использовать read_csv для чтения только определенных строк?

7

Error: User Rate Limit Exceeded

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  

Error: User Rate Limit Exceeded"TEST"Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Ваш Ответ

4   ответа
3

Error: User Rate Limit ExceededrowError: User Rate Limit Exceededcsv.readerError: User Rate Limit Exceeded

if not row[0].startswith('TEST'):
    process(row)
7
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…
Error: User Rate Limit Exceeded user1412286
2

Error: User Rate Limit Exceeded

skiprows : list-like or integer Row numbers to skip (0-indexed) or number of rows to skip (int)

Error: User Rate Limit Exceeded[0, 6]Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
0

import pandas as pd
import subprocess
grep = subprocess.check_output(['grep', '-n', '^TITLE', filename]).splitlines()
bad_lines = [int(s[:s.index(':')]) - 1 for s in grep]
df = pd.read_csv(filename, skiprows=bad_lines)

Похожие вопросы