1

Here is the code I am trying run:

import pandas as p
import locale
locale.setlocale(locale.LC_ALL, '')

Out[24]: 'Spanish_Argentina.1252'

from sqlalchemy import create_engine    
engine = create_engine('postgresql://user:pass@localhost/mdb',client_encoding = 'WIN1252')
p.read_sql("SELECT año FROM nacvivos.nac LIMIT 10",engine)

But a Had this encoding problem:

ProgrammingError: (ProgrammingError) no existe la columna «aã±o» LINE 1: SELECT año FROM nacvivos.nac LIMIT 10 ^ 'SELECT a\xc3\xb1o FROM nacvivos.nac LIMIT 10' {}

How should I set configuration in python for deal with this? Ignoring the obvious answer that modify the name of the column will solve the problem. Database in postgresql is win1252 encoding. Thanks in advance.

1 Answer 1

1

You have to specify the encoding keyword in create_engine (and not client_encoding), with 'cp1252' being the python codec name for 'WIN1252':

engine = create_engine('postgresql://user:pass@localhost/mdb', encoding='cp1252')

and then be sure your query is also encoded:

qry = u"SELECT año FROM nacvivos.nac LIMIT 10".encode('cp1252')
pd.read_sql(qry, engine)

With these two steps, I was able to read in a table with 'año' as a column name.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.