Python 将FeedParser对象序列化到Atom

Python 将FeedParser对象序列化到Atom,python,feedparser,atom-feed,Python,Feedparser,Atom Feed,我使用feedparser解析Atom提要,并对生成的Python对象进行一些操作。之后,我想将对象序列化回Atom。但是feedparser似乎没有提供这样做的方法 我注意到了其他原子库,比如gdata或demokritos,但说实话,它们对于初学者来说似乎非常困难。我使用feedparser正是因为它非常简单 基于namsral的良好响应,我用我最喜欢的模板语言SimpleTAL编写了一个序列化程序 import feedparser from simpletal import simpl

我使用feedparser解析Atom提要,并对生成的Python对象进行一些操作。之后,我想将对象序列化回Atom。但是feedparser似乎没有提供这样做的方法

我注意到了其他原子库,比如gdata或demokritos,但说实话,它们对于初学者来说似乎非常困难。我使用feedparser正是因为它非常简单

基于namsral的良好响应,我用我最喜欢的模板语言SimpleTAL编写了一个序列化程序

import feedparser

from simpletal import simpleTAL, simpleTALES, simpleTALUtils

mytemplate = """
<feed xmlns="http://www.w3.org/2005/Atom">
  <title tal:condition="feed/title" tal:content="feed/title"/>
  <link tal:condition="feed/link" tal:content="feed/link"/>
  <updated tal:condition="feed/updated" tal:content="feed/updated"/>
  <id tal:condition="feed/id" tal:content="feed/id"/>
  <!-- TODO other feed variables -->
  <entry xmlns='http://www.w3.org/2005/Atom'
       xmlns:thr='http://purl.org/syndication/thread/1.0'
       tal:repeat="entry entries">
    <title tal:condition="entry/title" tal:content="entry/title"/>
    <summary tal:condition="entry/summary" tal:content="entry/summary"/>
    <content tal:condition="entry/content" tal:content="python: entry.content[0]['value']"/> <!-- TODO: metadata and the other items in content -->
    <id tal:condition="entry/id" tal:content="entry/id"/>
    <published tal:condition="entry/published" tal:content="entry/published"/>
    <updated tal:condition="entry/updated" tal:content="entry/updated"/>
    <!-- TODO other entry fields -->
  </entry>
</feed>
"""
context = simpleTALES.Context(allowPythonPath=True)
template = simpleTAL.compileXMLTemplate (mytemplate)

class FeedParserPlus(feedparser.FeedParserDict):

    def serialize(self):
        context.addGlobal ("feed", self.feed)
        context.addGlobal ("entries", self.entries)
        result = simpleTALUtils.FastStringOutput()
        template.expand (context, result)
        return result.getvalue()

    @classmethod
    def parse(klass, text):
        result = feedparser.parse(text)
        return FeedParserPlus(result)
导入feedparser
从simpletal导入simpletal,simpleTALES,simpletalls
mytemplate=”“”
"""
context=simpleTALES.context(allowPythonPath=True)
template=simpleTAL.compileXMLTemplate(mytemplate)
类FeedParserPlus(feedparser.FeedParserDict):
def序列化(自):
context.addGlobal(“提要”,self.feed)
context.addGlobal(“条目”,self.entries)
结果=SimpleTalls.FastStringOutput()
template.expand(上下文、结果)
返回结果。getvalue()
@类方法
def解析(klass,文本):
结果=feedparser.parse(文本)
返回FeedParserPlus(结果)

使用像Mako、Jinja或Django这样的Python模板库生成提要相当容易

使用瓶子.py的示例:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>{{! d['title'] }}</title>
    <subtitle>{{! d['subtitle'] }}</subtitle>
    <link rel="alternate" type="text/html" href="{{! d['site_url'] }}" />
    <link rel="self" type="application/atom+xml" href="{{! d['feed_url'] }}" />
    <id>{{! d['feed_url'] }}</id>
    <updated>{{! d['date_updated'] }}</updated>
    <rights>{{! d['copyright'] }}</rights>

    %for entry in entries:
    <entry>
        <title>{{! entry['title'] }}</title>
        <link rel="alternate" type="text/html" href="{{! entry['url'] }}" />
        <id>{{! entry['atom_id'] }}</id>
        <published>{{! entry['date_published'] }}</published>
        <updated>{{! entry['date_updated'] }}</updated>
        <author>
            <name>{{! d['author'] }}</name>
            <uri>{{! d['site_url'] }}</uri>
        </author>
        <content type="html" xml:base="{{! d['site_url'] }}" xml:lang="en">
            <![CDATA[{{! entry['body'] }}]]>
        </content>
    </entry>
    %end

</feed>

{{!d['title']}
{{!d['subtitle']}
{{!d['feed_url']}
{{!d['date_updated']}
{{!d['copyright']}
%对于条目中的条目:
{{!条目['title']}
{{!条目['atom_id']}
{{!条目['date_published']}
{{!条目['date_updated']}