-1
我试过多种方法去除最终输出,但每次都是一样的。我不明白为什么,我的最终解决方案是在这个例子中:过滤器复制行不起作用
#!/usr/bin/python3
import re
import sys
import json
## Variables
fileOfHosts = "/path/to/file"
## Define lists and dics
my_dic = {}
resultOut = []
## Functions:
# Filter duplicated lines in list
def f1(seq):
newlist = []
for i in seq:
if i not in newlist:
newlist.append(i)
return newlist
# Json converter
def f2(inout):
out = json.dumps(inout)
return out
## Open file as list of lines
with open(fileOfHosts) as fileOfHosts:
result = list(fileOfHosts)
## Parse lines and generate dictionary
for f in f1(result):
sortByWord = re.findall(r"[\w+\.']+", f)
listOfTwo = sortByWord[:2]
if len(listOfTwo) == 2:
my_dic[listOfTwo[0]] = listOfTwo[1]
resultOut.append(my_dic.copy())
## Display list of dictoary as json
print(f2(resultOut))
我也试过在字典列表的末尾过滤。但总是我有相同的重复行。
有人可以提供一个更好的解决方案来筛选出重复?
编辑:这一切
首先是不是在评论中提到的问题的副本。在发布之前,我尝试过提到的解决方案。
实际上似乎问题不在于重复删除方法,而是在创建字典时进行重复。
代码:
for f in result:
sortByWord = re.findall(r"[\w+\.']+", f)
# print(sortByWord)
listOfTwo = sortByWord[:2]
# print(listOfTwo)
if len(listOfTwo) == 2:
print(listOfTwo)
my_dic[listOfTwo[0]] = listOfTwo[1]
resultOut.append(my_dic)
输出(打印(listOfTwo)):
['define', 'host']
['host_name', 'HOST_name']
['alias', 'HOST_name']
['address', '127.0.0.1']
['register', '1']
['timezone', 'Europe']
['use', 'user']
['_SNMPCOMMUNITY', 'public']
['_SNMPVERSION', '3']
['_HOST_ID', '184']
['define', 'host']
['host_name', 'HOST_name']
['alias', 'HOST_name']
['address', '127.0.0.1']
['register', '1']
['timezone', 'Europe']
['use', 'user']
['_SNMPCOMMUNITY', 'public']
['_SNMPVERSION', '3']
['_HOST_ID', '185']
输出(打印(F2(resultOut))):
[{"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}, {"_HOST_ID": "185", "address": "127.0.0.1", "_SNMPCOMMUNITY": "public", "timezone": "Europe", "define": "host", "host_name": "host", "_SNMPVERSION": "3", "alias": "host", "register": "1", "use": "user"}]
我不理解为什么。
没有阅读所有的代码,该函数'set'只返回独特的元素,所以'名单(套(...))'也许可以帮助? – Sosel
可能的重复[如何在保留顺序的同时从列表中删除重复项?](https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-同时保留顺序) – Mark
请在你的问题中添加一些示例行。 –