python in Loop循环为列表中的每个对象循环5次

发布于 2025-01-19 23:09:19 字数 1741 浏览 2 评论 0原文

所以我有一个大的 JSON 数组,看起来像这样:

[
  { "name": "(((", "handle": "/handle/20.500.12657/50523" },
  {
    "name": "01 Contents; Indian Recordings (Schomerus 1929)",
    "handle": "/handle/20.500.12657/24765"
  },
  {
    "name": "01 The Complete Historical Collections 1899\u20131950, Indian Recordings (Schomerus 1929)",
    "handle": "/handle/20.500.12657/24768"
  },
  {
    "name": "02 Guiding principles of the edition; Indian Recordings (Schomerus 1929)",
    "handle": "/handle/20.500.12657/24770"
  },
  {
    "name": "03 Editors' preface; Indian Recordings (Schomerus 1929)",
    "handle": "/handle/20.500.12657/24769"
  },
  ...
]

在 python 中,我加载它并循环遍历它,如下所示:

import json;

handles = open('data/handles.json');
data = json.load(handles);

for i in data:
    id = str(uuid.uuid4().fields[-1])[:10];
    try:
      #retrieves content from external source
      document = {
            '_id': id,
            'name': name,
            'description': description,
            'author': authors,
            'isbn': isbn,
            'language': lang,
            'license': license,
            'license_url': licenseUrl,
            'date_published': date,
            'location': location,
            'keywords': keywords,
            'publishing': publishing,
            'publishing_url': publishing_url,
            'category': category
          };

          ins = db.insert_one(document);
        except Exception as e:
          # log error

json 数组由 13 000 个唯一对象组成,data 的长度正是与 json 文件中的对象数量相同,但是当我查看数据库时,每个对象都有 5 个重复项。所有 json 数据都是 100% 唯一的,所以我真的不明白这是怎么可能的,我循环遍历它是错误的吗?循环内部的东西不应该影响它循环的次数,所以我不知道问题出在哪里。

另外,我可能相关的是:该代码在 Ubuntu 服务器上运行,并以 pm2 启动。 –

So I have a large JSON array that looks something like this:

[
  { "name": "(((", "handle": "/handle/20.500.12657/50523" },
  {
    "name": "01 Contents; Indian Recordings (Schomerus 1929)",
    "handle": "/handle/20.500.12657/24765"
  },
  {
    "name": "01 The Complete Historical Collections 1899\u20131950, Indian Recordings (Schomerus 1929)",
    "handle": "/handle/20.500.12657/24768"
  },
  {
    "name": "02 Guiding principles of the edition; Indian Recordings (Schomerus 1929)",
    "handle": "/handle/20.500.12657/24770"
  },
  {
    "name": "03 Editors' preface; Indian Recordings (Schomerus 1929)",
    "handle": "/handle/20.500.12657/24769"
  },
  ...
]

in python I load it and loop through it like this:

import json;

handles = open('data/handles.json');
data = json.load(handles);

for i in data:
    id = str(uuid.uuid4().fields[-1])[:10];
    try:
      #retrieves content from external source
      document = {
            '_id': id,
            'name': name,
            'description': description,
            'author': authors,
            'isbn': isbn,
            'language': lang,
            'license': license,
            'license_url': licenseUrl,
            'date_published': date,
            'location': location,
            'keywords': keywords,
            'publishing': publishing,
            'publishing_url': publishing_url,
            'category': category
          };

          ins = db.insert_one(document);
        except Exception as e:
          # log error

The json array consists of 13 000 unique objects, the length of the data is exactly the same as number of the objects in the json file, however when I look into DB, for every object there are exactly 5 duplicates. All of the json data is 100% unique, so I don't really see how that is possible, am I looping through it wrong ? The stuff inside of the loop shouldn't affect how many times it loops, so I dunno where is the problem.

Also, what I might be relevant: The code runs on Ubuntu server and is started with pm2. –

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文