[Tutor] FW: output not in ANSI, conversing char set to locale.getpreferredencoding()

eryksun eryksun at gmail.com
Thu Aug 16 15:02:44 CEST 2012


On Thu, Aug 16, 2012 at 7:33 AM, leon zaat <zaatlob at hotmail.com> wrote:
>
> Tried it with:
> openbareruimtenaam = openbareruimtenaam1.decode("UTF-8").encode("cp1252")
> but still the complains about the ascii error

Did you also change it for woonplaatsnaam? For example:

~~~~
import locale
file_encoding = locale.getpreferredencoding()  #e.g. "cp1252"
db_encoding = "utf-8"

        for row in num:
            openbareruimtenaam1 = row[0].decode(db_encoding)
            openbareruimtenaam = openbareruimtenaam1.encode(file_encoding)
            woonplaatsnaam1 = row[0].decode(db_encoding)
            woonplaatsnaam = woonplaatsnaam1.encode(file_encoding)
            newrow = [openbareruimtenaam, woonplaatsnaam]
            verblijfhoofd.writerow(newrow)

# Should "woonplaatsnaam1" be assigned to row[1] instead of row[0]?
~~~~

Off -topic comments:

Try to avoid cluttering your code with redundant parentheses, unless
they make it easier to understand a complex expression. If func()
returns a string, you don't need to do "string" + (func()). The extra
parentheses are just noise. Use "string" + func().

Also, if you have a long string, use the compiler's implicit string
join instead of runtime concatenation. If you have "string1"
"string2", the compiler stores "string1string2". You can combine this
with a parenthesized multiline expression. For example:

~~~~
        sql = ("Select identificatie, hoofdadres, verblijfsobjectgeometrie "
               "from verblijfsobject where identificatie > '" +
               hulpIdentificatie)
~~~~


More information about the Tutor mailing list