0

I'm scraping a £ value in python and when I try to write it into an excel sheet the process breaks and I get the following error

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 0: ordinal not in range(128)

The £ sign is printing without any error in the cmd prompt. Could some suggest how I can write the value (£1,750) into my sheet (with or without £ sign). many thanks...

import requests
from bs4 import BeautifulSoup as soup
import csv


outputfilename = 'Ed_Streets2.csv'

outputfile = open(outputfilename, 'wb')
writer = csv.writer(outputfile)
writer.writerow([Rateable Value])

url = 'https://www.saa.gov.uk/search/?SEARCHED=1&ST=&SEARCH_TERM=city+of+edinburgh%2C+EDINBURGH&ASSESSOR_ID=&SEARCH_TABLE=valuation_roll_cpsplit&PAGE=0&DISPLAY_COUNT=100&TYPE_FLAG=CP&ORDER_BY=PROPERTY_ADDRESS&H_ORDER_BY=SET+DESC&ORIGINAL_SEARCH_TERM=city+of+edinburgh&DRILL_SEARCH_TERM=BOSWALL+PARKWAY%2C+EDINBURGH&DD_TOWN=EDINBURGH&DD_STREET=BOSWALL+PARKWAY#results'

response = session.get(url)                 
html = soup(response.text, 'lxml')
prop_link = html.find_all("a", class_="pagelink button small")

for link in prop_link:
     prop_url = base_url+(link["href"])

     response = session.get(prop_url)
     prop = soup(response.content,"lxml")

     RightBlockData = prop.find_all("div", class_="columns small-7 cell")
     Rateable_Value = RightBlockData[0].get_text().strip()

     print (Rateable_Value)

     writer.writerow([Rateable_Value])
Owen
  • 169
  • 4
  • 16

1 Answers1

1

You need to encode your unicode object into bytes explicitely. Or else, your system will automatically try to encode it using ascii codec, which will fail with non-ascii characters. So, this:

Rateable_Value = Rateable_Value.encode('utf8')

before you

writer.writerow([Rateable_Value])

Should do the trick.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • This worked albeit with a  icon a the start which I can mass delete from my excel sheet. thank you !! – Owen Mar 13 '17 at 12:59