2
這個問題似乎是答案,但方式不同。我想跳過前兩行,因爲它們只是描述,而在第三行,我想忽略#符號,但沒有數據,因爲我想讀取和比較這些數據作爲列名。如何在Python中啓動csv文件時巧妙地處理#標記
# 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
對跳轉行,我知道我可以做這樣的事情
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
,並做列的單元測試文件名字我這樣做
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']
)
,但由於我正在做一些單元測試,因此我想忽略#標記第三行而不是其餘數據。
temp_data 1 temp_flow 2 temp_record 3 temp_all
任何幫助將非常感激。 Thanx很多