我是 Python 的新手,尝试使用 csv.reader 导入 2 个 csv 文件,然后比较一个文件中的元素是否存在于另一个文件中,如果存在,则删除整行。
我发现了类似问题的其他问题,这些问题表明列表理解是可行的方法,但是当我执行循环以检查 machine 中是否存在 appList 结果时我得到的是像 [] 这样的空括号。
到目前为止我的代码是:
import csv
appList = csv.reader(open('applist.csv', encoding = "ISO-8859-1"))
appList = list(appList)
machine = csv.reader(open('machine.csv', encoding = "ISO-8859-1"))
machine = list(machine)
for app in appList:
machine = [app for app in machine if app not in machine]
print(machine)
applist.csv 看起来像这样(它是 macOS 标准构建上的应用程序列表)
Adobe Creative Cloud for Enterprise
Adobe Acrobat DC Professional
Adobe Bridge CC
Adobe Extension Manager CC
Adobe Illustrator CC 2015
Adobe InDesign CC 2015
Adobe Photoshop CC 2015
Adobe Media Encoder CC 2015
AirPort Utility 6
App Store
Automator 2
[...]
machine.csv 看起来像这样......
"Application name";"Metric";"Last used";"Requirement";"Entitlement state";"Remark"
"Adobe Creative Cloud for Enterprise (Mac)";"Installations";"2018-03-28T10:45:00+01:00";"1";"Not covered";""
"Adobe Acrobat DC Professional (Mac)";"Installations";"2018-03-22T17:08:00+00:00";"0";"No requirement";"Installation included in software bundle"
"Adobe Bridge CC (Mac)";"No license required";"2018-03-12T13:45:00+00:00";"";"";"Installation included in software bundle"
"Adobe Extension Manager CC (Mac)";"No license required";"";"";"";"Installation included in software bundle"
"Adobe Illustrator CC 2015 (Mac)";"Installations";"2018-03-12T13:41:00+00:00";"0";"No requirement";"Installation included in software bundle"
[更新添加]
我目前的代码:
#!/usr/local/bin/python3
import os
import csv
def csv_reader(machine_dir, machine):
mach_list = list(csv.reader(open(machine_dir + "/" + machine, encoding="ISO-8859-1"), delimiter=";"))
return mach_list
def main():
# Get the paths to the csv files
csvFile = input("drop the app list csv here: ")
machine_dir = input("drop the machines csv folder here: ")
# Import appList csv
app_list = list(csv.reader(open(csvFile, encoding = "ISO-8859-1")))
# Get list of machine csv
machines = os.listdir(machine_dir)
for machine in machines:
machine_list = csv_reader(machine_dir, machine)
new_machine = [app for app in app_list if app not in machine_list]
print(new_machine)
if __name__ == '__main__': main()
我目前正在一台机器上测试它的 csv 文件,返回结果不是从 machine_list 中减去 app_list 后剩下的结果
请您参考如下方法:
您正在使用常规循环,然后进行列表理解,我认为这不是您需要的。
在您的列表推导式中,您正在遍历 machine 中的值,然后如果值不在 machine 中则将值附加到列表中。所以你的逻辑有点不对。实际上,您需要在列表理解中遍历 appList 的值,并查看它们是否出现在列表 machine 中:
import csv
appList = csv.reader(open('applist.csv', encoding = "ISO-8859-1"))
appList = list(appList)
machine = csv.reader(open('machine.csv', encoding = "ISO-8859-1"))
machine = list(machine)
new_machine = [app for app in appList if app not in machine]
编辑:
打开文件时,如果您检查它们,它们就是嵌套列表。一种解决方案可能是展平列表,然后使用相同的列表理解:
import csv
appList = csv.reader(open('applist.csv'))
appList = list(appList)
machine = csv.reader(open('machine.csv'))
machine = list(machine)
# Flatten both appList and machine
flat_appList = [item for sublist in appList for item in sublist]
flat_machine = [item for sublist in machine for item in sublist]
new_machine = [app for app in flat_machine if app not in flat_appList]
注意:小心 - 在示例 csv 文件中,appList.csv 包含例如Adobe Creative Cloud for Enterprise 与您机器中包含的内容不同.csv Adobe Creative Cloud for Enterprise (Mac)
