从python中的csv中删除双引号和括号

时间:2014-03-21 06:59:03

标签: python-2.7 csv

我试图在python中删除csv中的引号和括号,我尝试了下面的代码,但它无法给出正确的csv代码:

import json
import urllib2
import re
import os

from BeautifulSoup import BeautifulSoup
import csv

u = urllib2.urlopen("http://timesofindia.indiatimes.com/")
content = u.read()
u.close()

soup2 = BeautifulSoup(content)
blog_posts = []
for e in soup2.findAll("a", attrs={'pg': re.compile('^Head')}):
    for b in soup2.findAll("div", attrs={'style': re.compile('^color:#ffffff;font-size:12px;font-family:arial;padding-top:3px;text-align:center;')}):
        blog_posts.append(("The Times Of India",e.text,b.text))

print blog_posts
out_file = os.path.join('resources', 'ch05-webpages','newspapers','time1.csv')
f = open(out_file, 'wb')
wr = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
#f.write(json.dumps(blog_posts, indent=1))
wr.writerow(blog_posts)
f.close()

print 'Wrote output file to %s' % (f.name, )

csv看起来像:

"('The Times Of India', u'Missing jet: Air search expands to remote south Indian Ocean', u'Fri, Mar 21, 2014 | Updated 11.53AM IST')",

但我想要这样的csv:

The Times Of India,u'Missing jet: Air search expands to remote south Indian Ocean, u'Fri, Mar 21, 2014 | Updated 11.53AM IST

那么我能做些什么来获得这种类型的csv?

1 个答案:

答案 0 :(得分:1)

Writer.writerow()期望包含字符串或数字的序列。你正在传递一系列元组。请改用Writer.writerows()