I have the following code:
import csv
import subprocess
from subprocess import check_output
# Writing the pacman command output to file in csv format
sysApps = check_output(["pacman", "-Qn"])
sysAppsCSV = csv.DictReader(sysApps.decode('ascii').splitlines(),
delimiter=' ', skipinitialspace=True,
fieldnames=[ 'name', 'version']) # Thanks to https://stackoverflow.com/a/8880768/5565713 jcollado
with open('pacman.csv', 'w') as csvfile:
rows_sys = csv.writer(csvfile)
rows_sys.writerow(sysAppsCSV)
# Writing the pip command output in csv format
pipApps = check_output(["pip", "list"])
pipAppsCSV = csv.DictReader(pipApps.decode('ascii').splitlines(),
delimiter=' ', skipinitialspace=True,
fieldnames=[ 'name', 'version']) # Thanks to https://stackoverflow.com/a/8880768/5565713 jcollado
with open('pip.csv', 'w') as csvfile:
rows_pip = csv.writer(csvfile)
rows_pip.writerow(pipAppsCSV)
# Comparing the files
I want to compare the two files, not necessary the files it can also be the content of the variables already created, and get the result as diffs from pip.csv
file, practically I want to know what is in pip.csv
and is not in pacman.csv
. The example from here doesn't applies to my situation, but I will output the result in a similar way by listing the name and version.
EDIT: @Greg Sadetsky Thanks for the suggestion I used your example to simplify my code, but doesn't resolves my problem, I can't compare lists that way. I made some progress but I'm still not getting the desired output:
import csv
import subprocess
from subprocess import check_output
#Initializing variables
results_sys = ""
results_pip = ""
# Running the linux commands
sys_apps = set(check_output(["pacman", "-Qn"]).splitlines())
pip_apps = set(check_output(["pip", "list"]).splitlines())
# Saving the outputs of the commands in to a CSV format
for row in sys_apps:
result = row.decode('ascii').split(sep=" ")
with open('pacman.csv', 'a') as csvfile:
rows_sys = csv.writer(csvfile)
rows_sys.writerow(result)
for row in pip_apps:
result = row.decode('ascii').split(sep=" ")
with open('pip.csv', 'a') as csvfile:
rows_sys = csv.writer(csvfile)
rows_sys.writerow(result)
# Opening the files and comparing the results
with open('pacman.csv', 'r') as pacmanCSV:
sys_apps = pacmanCSV.readlines()
for row in sys_apps:
apps = row.split(",")
results_sys = results_sys + " " + apps[0]
with open('pip.csv', 'r') as pipCSV:
pip_apps = pipCSV.readlines()
for row in pip_apps:
apps = row.split(",")
results_pip = results_pip + " " + apps[0]
results_final = "List of apps installed from pip:\n################################"
for val in results_pip:
if val not in results_sys:
results_final = results_final + "\n" + val
print(results_final)
When I run this code I'm getting some capital letters, example: Imgur
ok so after reading about set I did this:
r1 = set(results_pip)
r2 = set(results_sys)
print(r1 - r2)
But I get similar results, only the first letters in caps appear.