You can use the csv
module to read and manipulate CSV files, and os
to check if the second file exists and read it as well. Here's some sample code:
main.py687 chars26 lines
This code reads file1.csv
, concatenates the first two columns with a dash, and then stores the result along with the remaining columns in a list of lists called lines
.
Next, it checks if file2.csv
exists. If it does, the same process of reading, concatenating, and comparison is done on it. Rows that start with the concatenated value are not stored in the lines2
list of lists.
Finally, if file2.csv
exists, a new file is written with the updated content of lines2
using the csv.writer
object.
Note that this code assumes that the first row of each file contains headers and thus skips it when reading the files. If this is not the case, you may need to adjust the code accordingly.
gistlibby LogSnag