'Python BeautifulSoup Empty Rows in CSV File

I am working on a scraper to pull street names and zip codes from a site and all of that is working great and it builds a CSV file just fine for me. But when I open the CSV file in Excel the file will have a blank row than a row with a street name with the zip code in the next column just like I want. But next I have a blank row than a row with a street name and zip code beside it. And this just continues on all the way through the file which gives me a row with a street name and zip codes in row then the word none in the next row when imported into the PHPMyAdmin database. I want to get rid of the blank rows. Here is my code.

from bs4 import BeautifulSoup
import csv 

import urllib2

url="http://www.conakat.com/states/ohio/cities/defiance/road_maps/"

page=urllib2.urlopen(url)

soup = BeautifulSoup(page.read())

f = csv.writer(open("Defiance Steets1.csv", "w"))
f.writerow(["Street", "Zipcode"]) # Write column headers as the first line

links = soup.find_all('a')

for link in links:
    i = link.find_next_sibling('i')
    if getattr(i, 'name', None):
        a, i = link.string, i.string[1:-1] 
        f.writerow([a, i])


Solution 1:[1]

This worked for me (I added lineterminator ="\n"):

from BeautifulSoup import BeautifulSoup
import csv 

import urllib2

url="http://www.conakat.com/states/ohio/cities/defiance/road_maps/"

page=urllib2.urlopen(url)

soup = BeautifulSoup(page.read())


f = csv.writer(open("Defiance Steets1.csv", "w"), lineterminator ="\n")
f.writerow(["Street", "Zipcode"]) # Write column headers as the first line

#print soup.
links = soup.findAll('a')

for link in links:
    #i = link.find_next_sibling('i')
    i = link.findNextSibling('i')
    if getattr(i, 'name', None):
        a, i = link.string, i.string[1:-1] 
        print [a,i]
        f.writerow([a, i])

Solution 2:[2]

this works for me... thanks if you have the writer and open in different lines, put it as a param in the writer function...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ernst
Solution 2 Andres Martinez