简体   繁体   中英

How to data print data to CSV using Python

I created a python program to provide a diff file. Wanted to know what the best way to send the results of the diff into a .csv would be

Here's my code,

import difflib

file1 = "swiss1.csv"
file2 = "swiss2.csv"

diff = difflib.ndiff(open(file1).readlines(),open(file2).readlines())

for line in diff:
    if line[0] in ["+", "-"]:
        print line

Rather than print to the terminal I would like to print it to a CSV file. Thoughts?

import difflib
import csv
file1 = "swiss1.csv"
file2 = "swiss2.csv"

diff = difflib.ndiff(open(file1).readlines(),open(file2).readlines())
f = open('changes.csv','w')
for line in diff:
    if line[0] in ["+", "-"]:
        f.write(line)
f.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM