Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8392571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:32:54+00:00 2026-06-09T19:32:54+00:00

When I use this code (adapted from Stephen Holiday code – thanks, Stephen for

  • 0

When I use this code (adapted from Stephen Holiday code – thanks, Stephen for your code!):

#!/usr/bin/env python    
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-

"""
USSSALoader.py
"""
import os
import re
#import urllib2
from zipfile import ZipFile
import csv
import pickle

def getNameList():
    namesDict=extractNamesDict()
    maleNames=list()
    femaleNames=list()
    for name in namesDict:
        counts=namesDict[name]
        tuple=(name,counts[0],counts[1])
        if counts[0]>counts[1]:
            maleNames.append(tuple)
        elif counts[1]>counts[0]:
            femaleNames.append(tuple)
    names=(maleNames,femaleNames)
    return names

def extractNamesDict():
    zf=ZipFile('names.zip', 'r')
    filenames=zf.namelist()

    names=dict()
    genderMap={'M':0,'F':1}

    for filename in filenames:
        file=zf.open(filename,'r')
        rows=csv.reader(file, delimiter=',')

        for row in rows:
            name=row[0].upper()
           # name=row[0].upper().encode('utf-8')
            gender=genderMap[row[1]]
            count=int(row[2])

            if not names.has_key(name):
                names[name]=[0,0]
            names[name][gender]=names[name][gender]+count

        file.close()
        print '\tImported %s'%filename
    return names

if __name__ == "__main__":
    getNameList()

I got this error:

  iterator = raw_query.Run(**kwargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 1622, in Run
    itr = Iterator(self.GetBatcher(config=config))
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 1601, in GetBatcher
    return self.GetQuery().run(_GetConnection(), query_options)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 1490, in GetQuery
    filter_predicate=self.GetFilterPredicate(),
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 1534, in GetFilterPredicate
    property_filters.append(datastore_query.make_filter(name, op, values))
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_query.py", line 107, in make_filter
    properties = datastore_types.ToPropertyPb(name, values)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore_types.py", line 1745, in ToPropertyPb
    pbvalue = pack_prop(name, v, pb.mutable_value())
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore_types.py", line 1556, in PackString
    pbvalue.set_stringvalue(unicode(value).encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 1: ordinal not in range(128)

This happens when I have names with non-ASCII caracters (like “Chávez” or “Barañao”). I tried to fix this problem doing this:

     for row in rows:
           # name=row[0].upper()
            name=row[0].upper().encode('utf-8')
            gender=genderMap[row[1]]
            count=int(row[2])

But, then, I got this other error:

 File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\USSSALoader.py", line 17, in getNameList
    namesDict=extractNamesDict()
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\USSSALoader.py", line 43, in extractNamesDict
    name=row[0].upper().encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 3: ordinal not in range(128)

I also tried this:

def extractNamesDict():
    zf=ZipFile('names.zip', 'r', encode='utf-8')
    filenames=zf.namelist()

But ZipFile doesn’t have such argument.

So, how to fix that avoiding this UnicodeDecodeError for non-ASCII names?

I’m using this code with GAE.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:32:55+00:00Added an answer on June 9, 2026 at 7:32 pm

    It looks like your first traceback is AppEngine-related. Are you building a loader that will populate the datastore? If so, seeing the code that comprises the models and does the put‘ing would be helpful. I will probably be corrected by someone, but in order for that piece to work I believe you actually need to decode instead of encode (i.e. when you read the sheet prior to the put, convert the string to unicode by using decode('utf-8') or decode('latin1'), depending on your situation).

    As far as your local code, I won’t pretend to know the deep internals of Unicode handling, but I’ve generally used decode() and encode() to handle these types of situations. I believe the correct encoding to use depends on the underlying text (meaning you’d need to know if it were encoded utf-8 or latin-1, etc.). Here is a quick test with your example:

    >>> s = 'Chávez'
    >>> type(s)
    <type 'str'>
    >>> u = s.decode('latin1')
    >>> type(u)
    <type 'unicode'>
    >>> e = u.encode('latin1')
    >>> print e
    Chávez
    

    In this case, I needed to use latin1 to decode the encoded string (I was using the terminal), but in your situation using utf-8 may very well work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code to select password field from the sql server 2012 db,
I use this code to process a date string coming in from a json
I use this code which is taken from MVC futures and attach the Attribute
Take the code below, adapted from this question : //Borrowed from another question because
I use this code : MethodInvoker TileLoadCompleteMethodInvoker = delegate() { CleanStatusLabelTimer.Enabled = true; MemoryTextBox.Text
i use this code in my app. just found is not correct when compare
I use this code to connect to SQL Server 2012, but it does not
I use this code , to log $_SERVER['REMOTE_ADDR']; to my small db my issue
I use this code to update data in database table. Can reuse same code
I use this code: http://blogswizards.com/plugin-development/sliding-boxes-and-captions-with-jquery On a simple gallery site I am building. Specifically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.