-1

I have the following piece of code:

# -*- coding: utf-8 -*-
import csv, geocoder

def extract_address(lat="", lng=""):
    if lat and lng:
        g = geocoder.google([lat, lng], method='reverse')
        if g:
            if g.street is not None:
                postal = g.postal
                housenumber = g.housenumber
                if g.housenumber is None:
                    housenumber = ""
                if g.postal is None:
                    postal = ""
                address = str(g.street.encode("utf-8")) + ", " + str(housenumber.encode("utf-8")) + ", " + str(postal.encode("utf-8")) + " " + str(g.city.encode("utf-8")) + ", " + str(g.country_long.encode("utf-8"))
                print type(address)
                return address.decode('utf-8')

with open('my_prueba.csv', 'w') as t:
    spamwriter = csv.writer(t, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
    spamwriter.writerow(['st_y', 'st_x', 'Address'])
    address = extract_address('some_lat', 'some_lng'))
    print address
    spamwriter.writerow([str(lat), str(lng), str(address.encode('utf-8'))])

this is the output:

<type 'str'>
Calle Hernán Cortés, 16, 28004 Madrid, Spain

but what I get once I open the csv file is this:

enter image description here

I've been trying many things, and reading different related stackoverflow posts, but nothing...can anyone give me a hand with this?!

Thanks in advance!

EDIT: I want to then convert it to a Excel file. If I open the csv with another app, the columns are separated in another way I don't want. My code above gives me the right separation when I open the csv file with excel

wj127
  • 118
  • 1
  • 12
  • What do you see if you open the csv with a text editor? – roganjosh Sep 15 '17 at 12:38
  • Have you tried using `ANSI` encoding instead of UTF-8? A quick readup here lead me to this answer: http://www.accompa.com/kb/answer.html?answer_id=264 – Sasha Sep 15 '17 at 12:39
  • use `string.encode('ascii','ignore')` – Anurag Misra Sep 15 '17 at 12:40
  • if I open it in 'Atom' for example, the accents are there, perfectly fine @roganjosh – wj127 Sep 15 '17 at 12:56
  • what do you mean @Jaxi ? I read your link, and I'm on a Mac. If I open it with the Numbers app, all columns are disordered and in a way I don't want :( – wj127 Sep 15 '17 at 12:59
  • I've tried the solution that they give in that post @JoshLee , but didn't worked for me as I want to conserve the separation as shown in the image above – wj127 Sep 15 '17 at 13:11

1 Answers1

1

THe accent are store in the csv file. No doubts. The bits correspond to text with utf8 encoding. You need to tell you csv reader (looks like MS Excel) that it is utf8.

For example with libreoffice: enter image description here

mquantin
  • 1,085
  • 8
  • 23
  • There's only a problem, if I do this, the last column (Address) gets separated, I mean: this address "Calle Hernán Cortés, 16, 28004 Madrid, Spain" instead of being in a single rectangle, gets splited in multiple rectangles... – wj127 Sep 15 '17 at 13:10
  • forget what I have just said @mquantin ! I can get the same result just selecting the "semicolon" Separator option! Many Thanks ^^ – wj127 Sep 15 '17 at 13:32