I am using python to generate a rss file,but when i call this method, it post a error"TypeError: writeRssFile() takes exactly 0 arguments (1 given)"

#!/usr/bin/env python2
# encoding: utf-8
import os,PyRSS2Gen
def writeRssFile(*newslist):
   item =[]
   for i in range(0,len(newslist)):
        item.append(PyRSS2Gen.RSSItem(
            title = newslist[i].get('title'),
            description = newslist[i].get('content'),
            pubDate = datetime.datetime.now()))
   rss = PyRSS2Gen.RSS2(
   title = "Andrew's PyRSS2Gen feed",
   link = "http://www.dalkescientific.com/Python/PyRSS2Gen.html",
   description = "The latest news about PyRSS2Gen, a "
                 "Python library for generating RSS2 feeds",
   lastBuildDate = datetime.datetime.now(),
   items = item[:],
   )

   rss.write_xml(open("pyrss2gen.xml", "w"))

i want call this method like this way:

newslist=[{'title':'title1','content':'content1'},{'title':'title2','content':'content2'}]
writeRssFile(newslist)

I had try googling on this but I'm not really very sure what is the exactly reason, so hopefully can get help from here. Thanks!

有帮助吗?

解决方案

You accept variable number of arguments. So, you need to unpack the list while calling, like this

writeRssFile(*newslist)

Also, you need to import datetime module.

Apart from that, when the range actually starts from 0, you can omit that. So,

range(0, len(newslist))

is the same as

range(len(newslist))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top