使用Python从JavaScript数组中删除重复项

2024-05-13 02:38:23 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个JavaScript元素数组,它看起来非常类似:

var oui = new Array({
    "pfx": "000000",
    "mask": 24,
    "desc": "00:00:00   Officially Xerox, but 0:0:0:0:0:0 is more common"
},{
    "pfx": "000001",
    "mask": 24,
    "desc": "Xerox  Xerox Corporation"
},{
    "pfx": "000002",
    "mask": 24,
    "desc": "Xerox  Xerox Corporation"
},{
    "pfx": "000003",
    "mask": 24,
    "desc": "Xerox  Xerox Corporation"
},{
    "pfx": "000004",
    "mask": 24,
    "desc": "Xerox  Xerox Corporation"
},{
    "pfx": "000004",
    "mask": 24,
    "desc": "Let's pretend this is a repeat"
   });

现在想象一下,文件非常大,一些“pfx”值在整个数据集中重复出现。显然,手动去重复是不可能的,所以我正在尝试找出最好的方法来编程实现它。如何编写一个python脚本来读取包含此数据集的.JS文件以消除重复并删除任何重复?换句话说,我想读入JS文件,解析数组,并生成另一个具有类似数组的JavaScript文件,但pfx变量只有唯一的值。我已经讨论了其他几个本质上类似的堆栈溢出问题,但似乎没有什么是非常适合这种情况的。在我的python测试中,我很少能让pfx变量自己删除重复项,或者python很难将其作为一个正确的JSON对象读入(即使没有“var”和“newarray”部分)。我还应该注意到,我在Python中对JS文件中的另一个JavaScript函数进行重复数据消除的原因(我在下面的例子中尝试了this)是因为它只是扩大了必须加载到页面上的JavaScript的大小。在未来,数组可能会继续增长——因此为了避免不必要的JavaScript加载以保持页面响应时间的快速,我认为这是一个可以而且应该脱机执行并添加到页面的步骤。你知道吗

为了澄清,这里有一个我试图模拟的网站模型:https://www.wireshark.org/tools/oui-lookup.html。它本质上很简单。你知道吗

感谢您的时间,并在先进的帮助!我真的很感激!你知道吗

研究:

Convert Javascript array to python list?

Remove duplicate values from JS array


Tags: 文件数据isvarjsmask页面数组
1条回答
网友
1楼 · 发布于 2024-05-13 02:38:23

因为结构不是嵌套的,所以可以用正则表达式匹配数组,然后用JSON解析它,用Python中的filter删除重复的对象,然后用消除重复的JSON字符串替换。你知道吗

使用数组文字语法([])而不是new Array来保持整洁(最好不要使用new Array):

import re
import json
str = '''
var oui = [{
    "pfx": "000000",
    "mask": 24,
    "desc": "00:00:00   Officially Xerox, but 0:0:0:0:0:0 is more common"
},{
    "pfx": "000001",
    "mask": 24,
    "desc": "Xerox  Xerox Corporation"
},{
    "pfx": "000002",
    "mask": 24,
    "desc": "Xerox  Xerox Corporation"
},{
    "pfx": "000003",
    "mask": 24,
    "desc": "Xerox  Xerox Corporation"
},{
    "pfx": "000004",
    "mask": 24,
    "desc": "Xerox  Xerox Corporation"
},{
    "pfx": "000004",
    "mask": 24,
    "desc": "Let's pretend this is a repeat"
   }];
'''

def dedupe(match):
   jsonStr = match.group()
   list = json.loads(jsonStr)
   seenPfxs = set()
   def notDupe(obj):
        thisPfx = obj['pfx']
        if thisPfx in seenPfxs:
            return False
        seenPfxs.add(thisPfx)
        return True
   return json.dumps([obj for obj in list if notDupe(obj)])

dedupedStr = re.sub(r'(?s)\[[^\]]+\](?=;)', dedupe, str)
print(dedupedStr)

输出:

var oui = [{"pfx": "000000", "mask": 24, "desc": "00:00:00   Officially Xerox, but 0:0:0:0:0:0 is more common"}, {"pfx": "000001", "mask": 24, "desc": "Xerox  Xerox Corporation"}, {"pfx": "000002", "mask": 24, "desc": "Xerox  Xerox Corporation"}, {"pfx": "000003", "mask": 24, "desc": "Xerox  Xerox Corporation"}, {"pfx": "000004", "mask": 24, "desc": "Xerox  Xerox Corporation"}];

如果可能的话,您可以考虑将数据存储在一个单独的标记中,而不是在内联Javascript中—这样更易于维护。例如,在HTML中,而不是

var oui = [{
    "pfx": "000000",
    "mask": 24,
    "desc": "00:00:00   Officially Xerox, but 0:0:0:0:0:0 is more common"
},{

考虑一下

var oui = JSON.parse(document.querySelector('[data-oui').textContent); console.log(oui);
<script data-oui type="application/json">[{
    "pfx": "000000",
    "mask": 24,
    "desc": "00:00:00   Officially Xerox, but 0:0:0:0:0:0 is more common"
}]</script>

这样就不必动态更改Javascript,只需更改<script data-oui type="application/json">标记。你知道吗

相关问题 更多 >