UnicodeEncodeError when running Json - python

Im getting a Unicode error when running this test script.
import urllib
import json
movieTitle = "Bee Movie"
title = movieTitle.replace(" ", "+")
year = ""
omdbAPI = "http://www.omdbapi.com/?t={}&y={}&plot=short&r=json".format(
title, year)
print (omdbAPI)
response = urllib.urlopen(omdbAPI)
data = json.loads(response.read())
valid_data = data["Response"]
print ("This data is: " + valid_data)
if valid_data == "True":
print data["Title"]
print data["Year"]
print data["Plot"]
print data["Rated"]
print data["Released"]
print data["Runtime"]
print data["Genre"]
print data["Director"]
print data["Writer"]
print data["Actors"]
print data["Language"]
print data["Country"]
print data["Awards"]
print data["Poster"]
print data["Metascore"]
print data["imdbRating"]
print data["imdbVotes"]
print data["imdbID"]
print data["Type"]
print data["Response"]
elif valid_data == "False":
print ("This data is: " + valid_data)
else:
raise ValueError("The information was not found")
Error :
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 19: ordinal not in range(128)
I guess its because one off the actors seams to have a é character.
I figured out that I could put .encode('utf8') after print data["Actors"] but it don't seams like the smartest thing to do.
I mean a random letter could occur on more places then on actor. And seams odd to go put .encode('utf8') after every instance
UPDATE :
Traceback (most recent call last):
File "/Volumes/postergren_projectDrive/Projekt/programmingSandbox/python/courses/udacity/Programming Foundations with Python/moveis/Advance/media.py", line 25, in <module>
print data["Actors"]
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 19: ordinal not in range(128)
[Finished in 0.1s with exit code 1]
[shell_cmd: "python" -u "/Volumes/postergren_projectDrive/Projekt/programmingSandbox/python/courses/udacity/Programming Foundations with Python/moveis/Advance/media.py"]
[dir: /Volumes/postergren_projectDrive/Projekt/programmingSandbox/python/courses/udacity/Programming Foundations with Python/moveis/Advance]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

Try this at the begining of your code:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

You can do this:
for key in data.keys()
data[key] = data[key].encode('utf8')

Related

How to return value from C program to Python script

When I try to return value from c code to python code i got error.
Traceback (most recent call last):
File "python.py", line 54, in <module>
print("\n\n\n\RESULT: ", str(result, "utf-8"))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 245: invalid start byte
in c function i try to return json string which got hex data - which I could parse in python and than make another calculation.
Example of returned string is "{"data":"0x123132"}"
In python i use
import ctypes
my_functions = ctypes.cdll.LoadLibrary("./my_functions.so")
my_functions.getJson.argtypes = (ctypes.c_char_p,)
my_functions.EthereumProcessor.restype = ctypes.c_char_p
result=my_functions.getJson()
print("\n\n\n\RESULT: ", str(result, "utf-8"))

UnicodeEncodeError: trying to print win32_process (wmi module)

i'm trying to print all the process ( like task mannager), so i used
this code :
from ctypes import *
import wmi
c = wmi.WMI()
kernel32 = windll.kernel32
i = 0
txt = ""
for process in c.Win32_Process():
try:
txt = txt + str(process)
except:
print process
most of the process (about 144/146) pass the try and then i print all together but there are about 2 process that fail the try, and the except and raise this error while i'm trying to print them:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 178-181: ordinal not in range(128)
someone know what to do?

Udacity Fundamental of programming with Python:

import urllib.request
def read_text():
quotes = open(r"C:\Users\Intel\Google Drive\Udacity\Full Stack\AbdoulCoverLetter.txt")
contents_of_files = quotes.read()
print(contents_of_files)
quotes.close()
check_profanity(contents_of_files)
My code is showing the following error:
UnicodeEncodeError: 'ascii' codec can't encode character '\u2022' in position 154: ordinal not in range(128)
I am not sure what is the issue. Please help..
def check_profanity(text_to_check):
with urllib.request.urlopen("http://www.wdylike.appspot.com/?q="+text_to_check) as response:
connection = response.read()
output = connection.read()
connection.close()
read_text()
I removed the bullet list and I changed the code. It is working fine now. Thanks..
import urllib.request
def read_text():
quotes = open(r"C:\Users\Intel\Google Drive\Udacity\Full Stack\AbdoulCoverLetter.txt")
contents_of_files = quotes.read()
print(contents_of_files)
quotes.close()
check_profanity(contents_of_files)
def check_profanity(text_to_check):
connection = urllib.request.urlopen("http://www.wdylike.appspot.com/?" + urllib.parse.urlencode([('q', text_to_check)]))
output = connection.read()
connection.close()
read_text()

Python.- fuzzy.DMetaphone 'ascii' error

How is that possible, that with the same input I sometime get ascii codec error, and sometime it works just fine? The code cleans the name and build it's Soundex and DMetaphone values. It works in ~1 out of 5 runs, sometimes more often :)
UPD: Looks like that's an issue of fuzzy.DMetaphone, at least on Python2.7 with Unicode. Plan to integrate Metaphone instead, for now. All solutions for fuzzy.DMetaphone problem are very welcome :)
UPD 2: Problem is gone after fuzzy update to 1.2.2. The same code works fine.
import re
import fuzzy
import sys
def make_namecard(full_name):
soundex = fuzzy.Soundex(4)
dmeta = fuzzy.DMetaphone(4)
names = process_name(full_name)
print names
soundexes = map(soundex, names)
dmetas = []
for name in names:
print name
dmetas.extend(list(dmeta(name)))
dmetas = filter(bool, dmetas)
return {
"full_name": full_name,
"soundex": soundexes,
"dmeta": dmetas,
"names": names,
}
def process_name(full_name):
full_name = re.sub("[_-]", " ", full_name)
full_name = re.sub(r'[^A-Za-z0-9 ]', "", full_name)
names = full_name.split()
names = filter(valid_name, names)
return names
def valid_name(name):
COMMON_WORDS = ["the", "of"]
return len(name) >= 2 and name.lower() not in COMMON_WORDS
print make_namecard('Jerusalem Warriors')
Output:
➜ python2.7 make_namecard.py
['Jerusalem', 'Warriors']
Jerusalem
Warriors
{'soundex': [u'J624', u'W624'], 'dmeta': [u'\x00\x00\x00\x00', u'ARSL', u'ARRS', u'FRRS'], 'full_name': 'Jerusalem Warriors', 'names': ['Jerusalem', 'Warriors']}
➜ python2.7 make_namecard.py
['Jerusalem', 'Warriors']
Jerusalem
Traceback (most recent call last):
File "make_namecard.py", line 38, in <module>
print make_namecard('Jerusalem Warriors')
File "make_namecard.py", line 16, in make_namecard
dmetas.extend(list(dmeta(name)))
File "src/fuzzy.pyx", line 258, in fuzzy.DMetaphone.__call__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xab in position 0: ordinal not in range(128)

Python 2 error:"Internal Python error in the inspect module

I have written this code to calculate the quadratic formula:
from numpy.lib.scimath import sqrt as csqrt
a = raw_input("a?")
b = raw_input("b?")
c = raw_input("c?")
def numcheck(x):
try:
i = float(x)
return True
except (ValueError, TypeError):
return False
if numcheck(a)==True:
a=int(a)
else:
print "a is not a number"
if numcheck(b)==True:
b=int(b)
else:
print "b is not a number"
if numcheck(c)==True:
c=int(c)
else:
print "b is not a number"
sqrt= ((b*b) - (4* (a*c)))
x_minus= (-b+(csqrt(sqrt)))/(2*a)
x_minus=str(x_minus)
x_plus= (-b-(csqrt(sqrt)))/(2*a)
x_plus=str(x_plus)
print "The solution is "+x_plus+" or "+x_minus
Never minding the rather crappy style, when the input is not a number, I get this error:
Traceback (most recent call last):
File "build\bdist.win32\egg\IPython\core\ultratb.py", line 776, in structured_traceback
File "build\bdist.win32\egg\IPython\core\ultratb.py", line 230, in wrapped
File "build\bdist.win32\egg\IPython\core\ultratb.py", line 267, in _fixed_getinnerframes
UnicodeDecodeError: 'ascii' codec can't decode byte 0xba in position 51: ordinal not in range(128)
ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.
Unfortunately, your original traceback can not be constructed.
Can anyone tell me why this is happening and, if possible, a way to fix it? Thanks.
To do a sqrt, you can simply do this:
def sqrt(num):
return num ** 0.5
I'll try to give you more info related to your problem, but for now try to replace your sqrt with that one.

Categories

Resources