2016-11-20 6 views
2

この質問は答えが異なるようです。私は最初の2行をスキップしたいのですが、3行目で#記号を無視したいのですが、このデータを列名として読んで比較したいからです。私はこのPythonのcsvファイルの先頭に#signをスマートに扱う方法

with open('kami.txt') as f: 
lines_after_2 = f.readlines()[2:] 

ような何かを行うことができ、それぞれの行番号またはeveryline

def read_data(data): 
with open(data, 'rb') as f: 
    data = [row for row in csv.reader(f.readlines())] 
return data 

でファイルを読み取り、列にユニットテストを行うには知っている、スキップライン用

# some description here 
# 1 is for good , 2 is bad and 3 for worse 
# 0 temp_data 1 temp_flow 2 temp_record 3 temp_all 

私はこれを行う名前

def test_csv_read_data_headers(self): 
    self.assertEqual(
     read_data(self.data)[0], 
     ['temp_data 1 temp_flow 2 temp_record 3 temp_all'] 
     ) 

しかし、私はいくつかのユニットテストをしているので、私は3行目の#記号を無視し、残りのデータはこれを無視したくありません。

temp_data 1 temp_flow 2 temp_record 3 temp_all 

本当にありがとうございます。 Thanx alot

答えて

0

パンダを試しましたか?

import pandas as pd 
df = pd.read_csv("kami.txt", header=None, skiprows = 2, names = [temp_data, 
temp_flow, temp_record, temp_all]) 
関連する問題