使用 XLRD 导入 Django 递归树

发布于 2024-09-10 07:41:05 字数 712 浏览 7 评论 0原文

我的数据(电子表格):

'1',,,
,'1.1',,
,,'1.1.1',
,,'1.1.2',
,,'1.1.3',
,'1.2',,
,'1.3',,
,,'1.3.1',
,,'1.3.2',
,,'1.3.3',
'2',,,
,'2.1',,
,,'2.1.1',
,,,'2.1.1.1'
,,,'2.1.1.2'
,,,'2.1.1.3'

我的模型:

class Vocabulary(models.Model):
    name = CharField(max_length=60)

class Concept(models.Model):
    parent = ForeignKey('self', blank=True, null=True)
    vocabulary = ForeignKey(Vocabulary)
    name = CharField(max_length=60)
    order = IntegerField(default=0)

我想要做什么:

def recurse(sheet):
    'Recurse outer edges of the tree saving concepts.'
        + 'Imply subtree order numbers. There are no numbers in the real data.'

My data (spreadsheet):

'1',,,
,'1.1',,
,,'1.1.1',
,,'1.1.2',
,,'1.1.3',
,'1.2',,
,'1.3',,
,,'1.3.1',
,,'1.3.2',
,,'1.3.3',
'2',,,
,'2.1',,
,,'2.1.1',
,,,'2.1.1.1'
,,,'2.1.1.2'
,,,'2.1.1.3'

My model:

class Vocabulary(models.Model):
    name = CharField(max_length=60)

class Concept(models.Model):
    parent = ForeignKey('self', blank=True, null=True)
    vocabulary = ForeignKey(Vocabulary)
    name = CharField(max_length=60)
    order = IntegerField(default=0)

What I am trying to do:

def recurse(sheet):
    'Recurse outer edges of the tree saving concepts.'
        + 'Imply subtree order numbers. There are no numbers in the real data.'

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2024-09-17 07:41:05

不容易弄清楚,只是分享一下。这就是我使用 Python XLRD 和 Django 将层次结构从 Excel 导入到简单邻接列表树存储的方法。

class XLRDParseError(Exception):
    """The XLS file was malformed."""

def load_xls(fname):
    """Import a hierarchy into the DB from Excel"""
    import xlrd
    xlrd.open_workbook(fname)
    firstSheet = book.sheet_by_index(0)
    v = Vocabulary(title='New Import')
    v.save()
    vid = Vocabulary.objects.get(id=v.id)
    conceptstack = []
    for row in range(firstSheet.nrows):
        blank = 0
        while True:
            cell = firstSheet.cell(row, blank)
            if cell.value:
                break
            blank += 1
        concept = Concept(vocabulary=vid, name=cell.value)
        concept.save()
        if len(conceptstack) < blank:
            raise XLRDParseError
        if len(conceptstack) > blank:
            for i in range(len(conceptstack) - blank):
                conceptstack.pop()
        if conceptstack:
            concept.parent = conceptstack[-1]
            concept.save()
        conceptstack.append(concept)

load_xls('/home/frank/top-navigation.xls')

关键字:层次结构、从 Excel 导入树、导入逗号/制表符分隔的层次结构、从 Excel 导入类别、Python Django XLRD 树导入

It wasn't easy to figure out, just to share it. This is how I imported hierarchy from Excel to simple adjacency list tree store using Python XLRD and Django.

class XLRDParseError(Exception):
    """The XLS file was malformed."""

def load_xls(fname):
    """Import a hierarchy into the DB from Excel"""
    import xlrd
    xlrd.open_workbook(fname)
    firstSheet = book.sheet_by_index(0)
    v = Vocabulary(title='New Import')
    v.save()
    vid = Vocabulary.objects.get(id=v.id)
    conceptstack = []
    for row in range(firstSheet.nrows):
        blank = 0
        while True:
            cell = firstSheet.cell(row, blank)
            if cell.value:
                break
            blank += 1
        concept = Concept(vocabulary=vid, name=cell.value)
        concept.save()
        if len(conceptstack) < blank:
            raise XLRDParseError
        if len(conceptstack) > blank:
            for i in range(len(conceptstack) - blank):
                conceptstack.pop()
        if conceptstack:
            concept.parent = conceptstack[-1]
            concept.save()
        conceptstack.append(concept)

load_xls('/home/frank/top-navigation.xls')

Keywords: hierarchy, import tree from Excel, import comma/tab delimited hierarchy, import categories from Excel, Python Django XLRD tree import

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文