我正在计算一组总统演讲中的缩略语的数量,并希望将这些缩略语输出到CSV或文本文件中。我的代码是:
import urllib2,sys,os,csv
from bs4 import BeautifulSoup,NavigableString
from string import punctuation as p
from multiprocessing import Pool
import re, nltk
import requests
import math, functools
import summarize
reload(sys)
def processURL_short(l):
open_url = urllib2.urlopen(l).read()
item_soup = BeautifulSoup(open_url)
item_div = item_soup.find('div',{'id':'transcript'},{'class':'displaytext'})
item_str = item_div.text.lower()
return item_str
every_link_test = ['http://www.millercenter.org/president/obama/speeches/speech-4427',
'http://www.millercenter.org/president/obama/speeches/speech-4424',
'http://www.millercenter.org/president/obama/speeches/speech-4453',
'http://www.millercenter.org/president/obama/speeches/speech-4612',
'http://www.millercenter.org/president/obama/speeches/speech-5502']
data = {}
count = 0
for l in every_link_test:
content_1 = processURL_short(l)
for word in content_1.split():
word = word.strip(p)
if word in contractions:
count = count + 1
splitlink = l.split("/")
president = splitlink[4]
speech_num = splitlink[-1]
filename = "{0}_{1}".format(president,speech_num)
data[filename] = count
print count, filename
with open('contraction_counts.csv','w',newline='') as fp:
a = csv.writer(fp,delimiter = ',')
a.writerows(data)
运行for
循环输出
79 obama_speech-4427
101 obama_speech-4424
101 obama_speech-4453
182 obama_speech-4612
224 obama_speech-5502
我想把它导出到一个文本文件中,左边的数字是一列,总统/演讲编号在第二列。我的with
语句只将每一行写入一个单独的文件,这绝对是次优的。在
您的问题是以
w
模式打开循环中的输出文件,这意味着每次迭代都会删除它。您可以通过两种方式轻松解决:将
open
置于循环之外(正常方式)。您将只打开一次文件,在每次迭代中添加一行,并在退出with
块时关闭它:以
a
(append)模式打开文件。每次迭代时,您都要重新打开文件并在末尾写入,而不是将其删除-由于打开/关闭,这种方式会使用更多的IO资源,并且只有在程序可能中断并且您希望确保在崩溃之前写入的所有内容都已实际保存到磁盘上时才使用你可以试试这样的方法,这是一个通用的方法,可以根据需要修改
或者如果是字典
^{pr2}$相关问题 更多 >
编程相关推荐