2017-01-23 70 views
-4

我有一個CSV文件,從中我兩兩件事:如何獲取.csv文件中的特定行並避免重複?

  1. 在B列,我需要得到只能用「錯誤」的內容。

  2. 完成此操作後,我需要從列G中獲取所有信息,同時避免重複。

例子:

## Level ## ## Message ## 

Error  ---------------  blah blah 


----------

我試圖使用PowerShell但是Python也將接受:

Param($Work) 

if (!$Work) { 
    powershell -NoExit -File $MyInvocation.MyCommand.Path 1 
    return 
} 

Select-String -pattern "ERROR" -path .\log.log 
+2

請編輯和格式化後,這樣的CSV包含合理的樣本數據。同時添加負面和正面結果。 – vonPryz

回答

0

沒有太知道什麼是列索引的列 'b' 和'克'是,但希望這有助於。

你可以閱讀更多的有關CSV處理在documentation

import csv 

#where the final answer will be 
extracted_info = [] 

with open('target.csv', 'r') as fd: 
    csv_reader = csv.reader(fd) 

    #Skip header 
    next(csv_reader, None) 

    #go through all rows 
    for row in csv_reader: 

     #Check if column b (looks like column 1?) 
     if (row[0] == "ERROR"): 
      #Get information from column 'G' 
      extracted_info.append(row[3]) 

#Get unique values only by casting to set then re-casting to list 
extracted_info = list(set(extracted_info)) 
0

試試這個

import-csv "c:\temp\youfile.csv" | where Level -eq 'Error' | select ColumnNameForG -Unique