Why is my urllib.quote in python encoding from Win-1252 instead of UTF-8 for CSV file? -


i've been trying url encode inputs them ready api request , urllib.quote works great string , encodes way it's supposed utf-8, when it's csv file, encodes in way api request not recognize.

# -*- coding: utf-8 -*- import urllib r = "handøl sweden" print urllib.quote(r) 

this returns correct format:

hand%c3%b8l%20sweden 

whereas:

# -*- coding: utf-8 -*-  import urllib import csv  citylist = []  open ('sitevalidate4.csv','rb') csvfile:     citydata = csv.reader(csvfile)     row in citydata:         citylist.append(row[12])         r = row[12] print r print urllib.quote(r) 

this returns:

handøl sweden hand%f8l%20sweden 

are there fixes encode input .csv file correct format?

your csv file encoded cp-1252, you'd have re-code utf-8:

r = r.decode('cp1252').encode('utf8') 

your plain python code using utf-8 bytes; provided code editor indeed saved data utf-8 coding: utf-8 header implies.

just putting pep 263 header in python source file doesn't magically make data read file utf-8 data too; it'll still need decoded correct codec for file.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -