【问题标题】:Show the title and content for all the Evernote notes with the same title显示所有同名印象笔记笔记的标题和内容
【发布时间】:2016-02-09 01:39:41
【问题描述】:

如何显示所有具有相同标题的笔记,并将其标题与内容一起显示。

你知道如何显示每条笔记的标题和内容吗? 如https://sandbox.evernote.com

我有一些具有相同标题的笔记,并希望将这些笔记的所有列表与其他笔记一起显示。

What the right combination of lists and dicts or other structures ?

我收到错误:列表索引必须是整数,而不是 str。

nb = []
nb.append([])
for note in result_list.notes:
    content = note_store.getNoteContent(auth_token, 
                                        note_store.getNote(note.guid, 
                                        True,False, False, False).guid)
    nb[note.guid].append([note.title, html2text.html2text(BeautifulSoup(ENMLToHTML(content), "html.parser").prettify()) ])
return render_to_response('oauth/callback.html', {'notebooks': notebooks, 
                                                  'result_list': result_list, 
                                                  'nb': nb})

.

<ul>
  {% for i,j,n in enumerate(nb) %}
      <li><b>{{ nb[i][j][n] }}</b><br>{{ nb[i][j][n+1] }}</li>
  {% endfor %}
</ul>

理想输出的样本:

  • 2600 杂志:黑客季刊
    任何对 Apple 产品的液体损坏都会使您的保修失效。

我已经有了以这种方式输出的工作代码,但是 目前:

每个标题只显示一个注释,

显示具有相同标题的所有笔记,并在内容旁边显示 guid(所有笔记都很好,但我需要在内容旁边输出标题)

p.s

又一次尝试:

title_contents = {}
    for note in result_list.notes:
        content = note_store.getNoteContent(auth_token, 
                                            note_store.getNote(note.guid, 
                                            True,False, False, False).guid)
        title_contents[ note.guid ] = [ note.title, html2text.html2text(BeautifulSoup(ENMLToHTML(content), "html.parser").prettify()) ]
    return render_to_response('oauth/callback.html', {'notebooks': notebooks, 
                                                      'result_list': result_list, 
                                                      'title_contents': title_contents})

Django 的 html:

  <ul>
    {% for content in title_contents.items %}
      <li><b>{{ content }}</b><br>{{ content }}</li>
    {% endfor %}
  </ul>

当前输出:

('c41c95b1-d2c5-481d-9fa6-34342371aba3', ['hi', u'hello *\n\n'])
('c41c95b1-d2c5-481d-9fa6-34342371aba3', ['hi', u'hello *\n\n'])

我尝试通过 index 获取标题和内容,例如:

  <ul>
    {% for content in title_contents.items %}
      <li><b>{{ content[0] }}</b><br>{{ content[1] }}</li>
    {% endfor %}
  </ul>

但得到另一个错误:

TemplateSyntaxError at /callback/

Could not parse the remainder: '[0]' from 'content[0]'

2016 年 2 月 10 日:

我开始使用 Node Js 编写这个网络应用程序,
并且所有的东西都在控制台中正常工作(当然,使用令牌 oauth,只显示文本,从文件中导入注释(简单解析),显示选择的注释等)。

我需要具有简单 Web 界面的类似应用程序,最好使用 Node Js Express。

在使用 Node Js Express 时,我开始用 Python (Django) 编写类似的东西。
我不知道哪个框架(Express 或 Django)更容易应对所有挑战。 这就是我从事这两个项目的原因。

EDAMTest.js 非常简单明了,但只适用于控制台环境。
在这种情况下,Node Js Express 对我来说比 Django 好得多,但我认为使用 Django 会更容易实现。

【问题讨论】:

标签: python django data-structures evernote


【解决方案1】:

如果我理解正确,您希望将所有具有相同标题的笔记的内容分组。这里首先要弄清楚的是数据结构。 python dict 听起来很合适,因为您关心笔记标题的映射。由于笔记标题可以映射到多个笔记,因此映射可能应该是从字符串(笔记标题)到字符串列表(具有该标题的所有笔记的笔记内容)。对于只有一个便笺具有该标题的情况(可能是大多数情况),便笺内容列表的大小为 1。

自从我编写 python/django 以来已经有一段时间了,但是要使用您的代码片段:

title_contents = {}
for note in result_list.notes:
    content = note_store.getNoteContent(auth_token, 
                                        note_store.getNote(note.guid, 
                                        True,False, False, False).guid)
note_content = html2text.html2text(BeautifulSoup(ENMLToHTML(content)).prettify())
existing_contents = title_contents.get(note.guid, [])
existing_contents.append(note_content)
title_contents[note.guid] = existing_contents

最后,我们有一个看起来像这样的数据结构

{ 'test title': ['foo contents'], 'untitled': ['one content', 'another content']}

现在,在您的模板中,您需要遍历其中的所有标题和所有注释。您可能需要更正我的语法,但它看起来像这样:

<ul>
    {% for title, content_list in title_contents %}
      <li><b>{{ title }}</b><br>
      {% for content in content_list %}
        {{ content }}<br/>
      {% endfor %}
    {% endfor %}
</ul>

希望这是有道理的!

【讨论】:

  • 感谢@akhaku,代码看起来很有趣,但我得到了错误:'list' object has no attribute 'push'
  • 我添加了 class List(list): def push(self, x): self.append(x) 但我仍然收到该错误
  • 如果我使用'append'而不是'push',这部分代码不会输出任何东西。
  • 抱歉,我做错了几件事。我打算使用list.append,最近做了太多的javascript。我还打算将现有内容放回 note_content,请参阅更新的答案。
【解决方案2】:

我终于找到了解决办法!!!

元组字典

...

note_store.getNoteContent(auth_token, note.guid)

...

<ul>
  {% for m,t in title_contents.items %}
    <li><b>{{ t.0 }}</b><br>{{ t.1 }}</li>
  {% endfor %}
</ul>

【讨论】:

    【解决方案3】:

    错误意味着note.guid 是一个字符串,并且由于您将它用作列表索引,因此它必须是一个整数。

    你可以使用 print(repr(note.guid), type(note.guid)) 这样您就可以看到它是什么并进行相应的调整。

    如果它是一个字符串,例如:'3',那么您可以将其转换为:int(note.guid)

    【讨论】:

    • 谢谢,但 note.guid 包含小写字符和“-”,例如:28aeb0dc-6080-43c2-a527-294e4cb66b51
    • 对,所以不能作为列表索引使用。您希望 nb 完成后的样子?
    • 也许您希望它 (nb) 成为字典,而不是列表,这看起来就像您在第二次尝试中所做的那样。另一个错误是什么?
    • TemplateSyntaxError at /callback/ 无法解析剩余部分:来自“内容 [0]”的“[0]”
    • 将字符串转换为 int 以进行索引通常不是一个好主意。如果您尝试使用字符串作为索引,通常应该使用映射(python 中的字典)。