Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »


https://pymotw.com/3/csv/


compare and write csv file
import csv

open_file1 = open ('./output/Narcos.S01E01_stats_translation.csv')
open_file2 = open ('./output/Narcos.S01E02_stats_translation.csv')
open_outputfile = open ('./output/Narcos.commun_words.csv','wt')

csv_f1 = csv.reader(open_file1)
csv_f2 = csv.reader(open_file2)
writer = csv.writer(open_outputfile, delimiter=',',lineterminator= '\r')

#loop thru the first file. one row at the time
for row_f1 in csv_f1:
	open_file2.seek(0)
	csv_f2 = csv.reader(open_file2)

	#loop thru the second file and compare one row at the time
	for row_f2 in csv_f2:
		#commun: print if value in file1 is in file2
		if row_f1 == row_f2: 
			print ("match:", row_f1)
			writer.writerow (row_f1)
		#diff: print if value in file1 is NOT in file2

open_file2.close()
open_file1.close()


  • No labels