可能重复:
Compare two different files line by line and write the difference in third file - Python
我头脑中的逻辑是这样的...... 对于import_file中的行,检查它是否包含Existing-user-string-list中的任何项,如果它包含该列表中的任何一项,则删除该行的该行。
filenew = open('new-user', 'r')
filexist = open('existing-user', 'r')
fileresult = open('result-file', 'r+')
xlines = filexist.readlines()
newlines = filenew.readlines()
for item in newlines:
if item contains an item from xlines
break
else fileresult.write(item)
filenew.close()
filexist.close()
fileresult.close()
我知道这段代码全都搞砸了,但也许你可以指出我正确的方向。
谢谢!
编辑----
以下是我现有用户文件中的内容示例....
allyson.knanishu
amy.curtiss
amy.hunter
amy.schelker
andrea.vallejo
angel.bender
angie.loebach
以下是我的新用户文件中的内容示例....
aimee.neece,aimee,neece,aimee.neece@faculty.asdf.org,aimee neece,aimee neece,"CN=aimee neece,OU=Imported,dc=Faculty,dc=asdf,dc=org"
alexis.andrews,alexis,andrews,alexis.andrews@faculty.asdf.org,alexis andrews,alexis andrews,"CN=alexis andrews,OU=Imported,dc=Faculty,dc=asdf,dc=org"
alice.lee,alice,lee,alice.lee@faculty.asdf.org,alice lee,alice lee,"CN=alice lee,OU=Imported,dc=Faculty,dc=asdf,dc=org"
allyson.knanishu,allyson,knanishu,allyson.knanishu@faculty.asdf.org,allyson knanishu,allyson knanishu,"CN=allyson knanishu,OU=Imported,dc=Faculty,dc=asdf,dc=org"
来自@mikebabcock的新代码......谢谢。
outfile = file("result-file.txt", "w")
lines_to_check_for = [ parser(line) for line in file("existing-user.txt", "r") ]
for line in file("new-user.txt", "r"):
if not parser(line) in lines_to_check_for:
outfile.write(line)
为解析器添加了一个import语句...我收到以下错误...
C:\temp\ad-import\test-files>python new-script.py
Traceback (most recent call last):
File "new-script.py", line 7, in <module>
lines_to_check_for = [ parser(line) for line in file("existing-user.txt", "r
") ]
TypeError: 'module' object is not callable
谢谢!
答案 0 :(得分:3)
假设我明白你想做什么....使用set intersection:)
for line in newlines:
if set(line.split()) & set(xlines): #set intersection
print "overlap between xlines and current line"
break
else:
fileresult.write(item)
答案 1 :(得分:1)
如果输入文件格式是每行有一个项目(以便检查readlines列表中现有元素的确定),那么您正在寻找列表成员资格测试:
if item in xlines:
break
指出一些更多python的东西:从你测试成员资格的列表中创建一个集合(因为测试将是对数时间而不是像列表一样的线性):
xlines = set(filexists.readlines())
此外,您可以使用with语句来避免关闭文件并提供更清晰的代码(如第一个示例here)。
答案 2 :(得分:0)
我认为这就是你想要做的事情:
outfile = file("outfile.txt", "w")
lines_to_check_for = [ line for line in file("list.txt", "r") ]
for line in file("testing.txt", "r"):
if not line in lines_to_check_for:
outfile.write(line)
这会将list.txt
中的所有行读入数组,然后针对该数组检查testing.txt
的每一行。不在该数组中的所有行都将写出outfile.txt
。
以下是基于您的新问题的更新示例:
newusers = file("newusers.txt", "w")
existing_users = [ line for line in file("users.txt", "r") ]
for line in file("testing.txt", "r"):
# grab each comma-separated user, take the portion left of the @, if any
new_users = [ entry.split["@"](0) for entry in line.split(",") ]
for user in new_users:
if not user in existing_users:
newusers.write(user)