0

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

6
  • What do you see if you open the csv with a text editor? Commented Sep 15, 2017 at 12:38
  • Have you tried using ANSI encoding instead of UTF-8? A quick readup here lead me to this answer: accompa.com/kb/answer.html?answer_id=264 Commented Sep 15, 2017 at 12:39
  • use string.encode('ascii','ignore') Commented Sep 15, 2017 at 12:40
  • if I open it in 'Atom' for example, the accents are there, perfectly fine @roganjosh Commented Sep 15, 2017 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 :( Commented Sep 15, 2017 at 12:59

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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...
forget what I have just said @mquantin ! I can get the same result just selecting the "semicolon" Separator option! Many Thanks ^^

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.