2016-09-28 55 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)