tags:

views:

41

answers:

2

Hello everyone i currently have this:

import feedparser
d = feedparser.parse('http://store.steampowered.com/feeds/news.xml')

for i in range(10):
    print d.entries[i].title
    print d.entries[i].date

How would i go about making it so that the title and date are on the same line? Also it doesn't need to print i just have that in there for testing, i would like to dump this output into a mysql db with the title and date, any help is greatly appreciated!

A: 

Regarding your actual question: if you want to join two strings with a comma you can use something like this:

print d.entries[i].title + ', ' + str(d.entries[i].date)

Note that I have converted the date to a string using str.

You can also use string formatting instead:

print '%s, %s' % (d.entries[i].title, str(d.entries[i].date))

Or in Python 2.6 or newer use str.format.

But if you want to store this in a database it might be better to use two separate columns instead of combining both values into a single string. You might want to consider adjusting your schema to allow this.

Mark Byers
+1  A: 

If you want to print on the same line, just add a comma:

print d.entries[i].title, # <- comma here
print d.entries[i].date

To insert to MySQL, you'd do something like this:

to_db = []
for i in range(10):
    to_db.append((d.entries[i].title, d.entries[i].date))

import MySQLdb
conn = MySQLdb.connect(host="localhost",user="me",passwd="pw",db="mydb")
c = conn.cursor()
c.executemany("INSERT INTO mytable (title, date) VALUES (%s, %s)", to_db)
Adam Bernier