使用dict_writer将行附加到csv

时间:2015-08-25 01:44:01

标签: python python-2.7 csv

我使用以下代码将行附加到现有csv文件:

counter=1
for thepage in newpages:    
    web_page = urllib2.urlopen(thepage)
    soup = BeautifulSoup(web_page.read())

    fieldnames=["var1", "var2","var3","var4","var5", "var6", "var7"]
    if counter==1:
            f = open('file.csv', 'wb')  
            my_writer = csv.DictWriter(f, fieldnames)
            my_writer.writeheader()
            f.close()

    variables={ele:"MISSING" for ele in fieldnames}
    variables['var1']=str(counter)
    variables['var2']=soup.find_all('strong')[0].text
    variables['var3']=soup.find_all('p')[1].text[0]
    variables['var4']=soup.find_all('p')[1].text[1]
    variables['var4']=soup.find_all('p')[2].text
    variables['var6']=soup.find_all('p')[6].text
    variables['var7']=soup.find_all('p')[7].text
    print variables
    with open('file.csv', 'r+b') as f:
        header = next(csv.reader(f))
        dict_writer = csv.DictWriter(f, header)
        dict_writer.writerow(variables) 
   counter+=1

在循环中创建“变量”字典。循环继续运行没有问题但是当我查看csv文件时,它已经停止在第30行中或多或少地追加行。我已经检查了它并不是“变量”字典的问题,即使我更改文件并使用其他变量,它也会在同一行中或多或少地失败。可能是什么问题呢?

1 个答案:

答案 0 :(得分:2)

我认为问题是你每次都要覆盖文件。它以模式/clubs/5678/people打开,这将文件指针定位到文件的开头。因此,后续写入会覆盖文件的先前内容。

您可以在追加模式下打开文件,可以使用模式//people_states.js var list = {/* object-style definition of people list state */ }; var detail = {/* object-style definition of people detail state */ }; module.constant('PeopleStates', { list: function(customUrl){ //param allows other states to use same definition with different URL var toReturn = angular.copy(list); toReturn.url = customUrl ? customUrl : toReturn.url; return toReturn; } detail: function(url){/* same idea */ } } //club_states.js //var list, detail... same idea module.constant('ClubStates', {/*same idea*/}) module.config(function($stateProvider, PeopleStates, ClubStates){ $stateProvider .state('clubs', ClubStates.list()) .state('clubs.detail', ClubStates.detail()) .state('clubs.detail.members', PeopleStates.list("/members")) .state('clubs.detail.members.detail', PeopleStates.detail()) //^^ so URL is /clubs/5678/members/1234 }) 完成。

话虽如此,没有必要不断打开文件,也不必在for循环体内输出CSV标头。我建议如下:

'r+b'