‘ascii’ codec can’t encode characters : ordinal not in range(128)

I work on a system which fetches data from a database (Postgres) using python & writes them into a csv file. Few days ago i came through an error which was showing “ascii codec can’t encode characters : ordinal not in range(128)” whenever i tried to fetch data from a particular date range.

Problem : 

When ever i tried to fetch data of a particular data range (suppose date range between 1-Jan-2013 to 31-Jan-2013) error popped up. although data fetching between other data range was working fine. That clearly meant that problem was in data of that date range only. Actually problem was in few entry of one of the column(s) (email) of my table, which was storing some Non-ASCII value . When python tried to write that value into the csv, it couldn’t process the value & error came up.

Solution :

Either you can make that value (data) unicode in python by  using:

email = [DefectedValue].encode('utf-8').strip()

Or you can find the data actually causing problems and remove them. for finding the defected data you can run following command in your PostgreSQL terminal.

SELECT * FROM YourTableName WHERE EffectedColumn similar to '%[^\x20-\x7e]+%' ;

You can update/delete that tuple which is storing Non-ASCII characters.