2016-09-28 58 views
0

這是一個文件result.csvPython的文件匹配,並附加

M11251TH1230 
M11543TH4292 
M11435TDS144 

這是另一個文件sample.csv

M11435TDS144,STB#1,Router#1 
M11543TH4292,STB#2,Router#1 
M11509TD9937,STB#3,Router#1 
M11543TH4258,STB#4,Router#1 

我可以寫一個Python程序來比較兩個文件,如果行result.csvsample.csv中該行中的第一個字匹配,然後附加1 else在sample.csv的每一行附加0?

+0

*追加1,否則追加0 * - 如果它看起來像'M11435TDS144,STB#1,#路由器11 M11543TH4292,STB#2,#路由器追加11'後? – RomanPerekhrest

+0

不,它應該看起來像M11435TDS144,STB#1,路由器#1,1和M11543TH4258,STB#4,路由器#1,0因爲M11543TH4258沒有在result.csv中找到 –

+0

是的,你可以編寫一個程序。請至少*嘗試*並自己編寫一些代碼,然後發佈。如果它仍然存在問題或者如果您對此有疑問,這並不重要,我們可以回答。堆棧溢出不在這裏爲你寫代碼。 – MichielB

回答

0

下面的代碼片段會爲你

import csv 

with open('result.csv', 'rb') as f: 
    reader = csv.reader(f) 
    result_list = [] 
    for row in reader: 
     result_list.extend(row) 
with open('sample.csv', 'rb') as f: 
    reader = csv.reader(f) 
    sample_list = [] 
    for row in reader: 
     if row[0] in result_list: 
      sample_list.append(row + [1]) 
     else: 
      sample_list.append(row + [0] 
with open('sample.csv', 'wb') as f: 
    writer = csv.writer(f) 
    writer.writerows(sample_list) 
+0

'f.close()'不是必需的,並且確實會是錯誤爲'with'本身會照顧關閉文件指針:-) – thiruvenkadam

0
import pandas as pd 

d1 = pd.read_csv("1.csv",names=["Type"]) 
d2 = pd.read_csv("2.csv",names=["Type","Col2","Col3"]) 
d2["Index"] = 0 

for x in d1["Type"] : 
    d2["Index"][d2["Type"] == x] = 1 

d2.to_csv("3.csv",header=False) 

考慮「1.csv」和「2.csv」工作是您的CSV輸入文件和「3.csv」是結果你需要

0

csv.reader使用和csv.writercsv模塊)的溶液:

import csv 

newLines = [] 
# change the file path to the actual one 
with open('./data/result.csv', newline='\n') as csvfile: 
    data = csv.reader(csvfile) 
    items = [''.join(line) for line in data] 

with open('./data/sample.csv', newline='\n') as csvfile: 
    data = list(csv.reader(csvfile)) 
    for line in data: 
     line.append(1 if line[0] in items else 0) 
     newLines.append(line) 

with open('./data/sample.csv', 'w', newline='\n') as csvfile: 
    writer = csv.writer(csvfile) 
    writer.writerows(newLines) 

項的sample.csv內容:

M11435TDS144,STB#1,Router#1,1 
M11543TH4292,STB#2,Router#1,1 
M11509TD9937,STB#3,Router#1,0 
M11543TH4258,STB#4,Router#1,0 
+0

Hi Roman,謝謝你的回覆。每次我執行代碼時,它都會繼續附加。而不是附加到sample.csv,當代碼執行時,我可以一次又一次地重寫最後一列。 –

+0

你好,我的代碼每次運行時都會覆蓋'sample.csv'文件的內容。沒有追加。在「寫入」模式下檢查您的代碼是否正確打開文件。關鍵的一行是'open('./ data/sample.csv','w',newline ='\ n')作爲csvfile:' – RomanPerekhrest

+0

Hi roman,謝謝你的回覆。它給「TypeError:'換行'是一個無效的關鍵字參數爲這個函數」和每次它附加到一個sample.csv文件,而不是重寫它 –

0

由於只有一列,我不知道爲什麼你做它作爲一個result.csv。如果它不會有更多的列,一個簡單的文件讀取操作就足夠了。隨着將數據從result.csv轉換爲字典,也將有助於快速運行。

result_file = "result.csv" 
sample_file = "sample.csv" 

with open(result_file) as fp: 
    result_data = fp.read() 
    result_dict = dict.fromkeys(result_data.split("\n")) 
    """ 
    You can change the above logic, in case you have very few fields on csv like this: 
    result_data = fp.readlines() 
    result_dict = {} 
    for result in result_data: 
     key, other_field = result.split(",", 1) 
     result_dict[key] = other_field.strip() 
    """ 

#Since sample.csv is a real csv, using csv reader and writer 
with open(sample_file, "rb") as fp: 
    sample_data = csv.reader(fp) 
    output_data = [] 
    for data in sample_data: 
     output_data.append("%s,%d" % (data, data[0] in result_dict)) 

with open(sample_file, "wb") as fp: 
    data_writer = csv.writer(fp) 
    data_writer.writerows(output_data)