Transform pubDate to string in Python

698 views Asked by At

I want to convert an integer (the pubDate from the RSS-Feed) to a string in Python because otherwise I can't save it to my *.tsv file. If I remove the pubDate snippet I don't have any problems.

#!/usr/bin/python

import feedparser
import codecs

f = codecs.open('html/bzusnews.tsv', 'a', 'utf-8')
f.write("col" + "\n")

d = feedparser.parse('http://www.buzzfeed.com/usnews.xml')


for e in d['entries']:


        f.write(e.get('pubDate') + "$$$$$" + e.get('title') + "$$$$$" + e.get('author') + "$$$$$" + e.get('link') +  "\n" )

#!/usr/bin/python

import feedparser
import codecs
import datetime



f = codecs.open('html/bzusnews.tsv', 'a', 'utf-8')
f.write("Spalte" + "\n")


d = feedparser.parse('http://www.buzzfeed.com/usnews.xml')

pubDate = "-".join(pubDate.split()[1:5])
datetime_pubDate = datetime.datetime.strptime(pubDate , "%d-%b-%Y-%H:%M:%S")

str_pubDate = datetime_pubDate.strftime("%Y-%m-%d %H:%M:%S")

for e in d['entries']:



        f.write(e.get('str_pubDate') + "\t" + e.get('title') + "\t" + e.get('author') + "\t" + e.get('link') +  "\n" )
1

There are 1 answers

2
roOt On BEST ANSWER

I have what you need now. For me it works:

#!/usr/bin/python

import feedparser
import codecs
import datetime

f = codecs.open('bzusnews.tsv', 'a', 'utf-8')
f.write("Spalte" + "\n")

feeds = feedparser.parse('http://www.buzzfeed.com/usnews.xml')

for feed in feeds.entries:
    f.write("\t".join((feed.title, feed.author, feed.published, feed.link)) + "\n")

f.close()