我有两个csv文件,1。master_test.csv 2. child_test1.csv 3. output.csv
master_test.csv包含以下信息
nnn
bbb_12
KKK
CCC
child_test1.csv包含以下信息
nnn
DDD
MMM
bbb_13
我需要将child_test1.csv数据与master_test.csv文件进行比较。最后写入output.csv文件。 我需要输出如下
nnn nnn匹配发现
ddd ---找不到主表
mmm ----未找到主表
bbb_13 bbb_12不匹配或版本更改
我是python的新手,截至目前我已尝试过以下代码, 我能够在同一个coloum中找到匹配[但我需要如上所述output.csv文件]
运行上面的代码后,输出为
NNN
匹配nnn
但我需要输出如下。
nnn nnn匹配发现
ddd ---找不到主表
mmm ----未找到主表
bbb_13 bbb_12不匹配或版本更改
请任何人都可以指导我
f1 = open("C:\\Python34\\master_test.csv", "r")
f2 = open("C:\\Python34\\child_test1.csv", "r")
outFile = open("C:\\Python34\\output.csv", "w")
fileOne = f1.readlines()
fileTwo = f2.readlines()
f1.close()
f2.close()
for i in fileOne:
for x in fileTwo:
if (x == i):
outFile.write(i +"Equal " + x)
答案 0 :(得分:1)
你可以做这样的事情
f1 = open("C:\\Python34\\master_test.csv", "r")
f2 = open("C:\\Python34\\child_test1.csv", "r")
outFile = open("C:\\Python34\\output.csv", "w")
fileOne = f1.readlines()
fileTwo = f2.readlines()
f1.close()
f2.close()
for i in fileOne:
for x in fileTwo:
writer = "{} --- Not found Master sheet".format(i)
if (x == i):
writer = "{} {} match found".format(i,x)
outFile.write(writer+'\n')
This will keep the message in writer always that BlahBlahBlah! not found un master, But in case it is found then the writer will change to Blah Blah match found
Hello
thank you for your reply,
But above code is not solved my issue,
f1 = open("C:\\Python34\\master_test.csv", "r")
f2 = open("C:\\Python34\\child_test1.csv", "r")
outFile = open("C:\\Python34\\Aoutput.csv", "w")
fileOne = f1.readlines()
fileTwo = f2.readlines()
f1.close()
f2.close()
outFile.write("Match found\n")
for i in fileOne:
for x in fileTwo:
if (x == i):
output.write(x)#Match found
output.write("Match not found")
#similar way if check
for i in fileOne:
for x in fileTwo:
if (x != i):
output.write(x)
#Its writing all the combination, I need only not match from child values i.e
DDD
MMM