• Register
0 votes
1.8k views

Problem :

I am new to the Python, I am using Python-2.6 CGI scripts but facing following error in the server log while I was doing json.dumps(),

Traceback (most recent call last):
File "/etc/mongodb/server/cgi-bin/getstats.py", line 135, in <module>
print json.dumps(​​__get​data())
File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa5 in position 0: invalid start byte

​Please Note :

​__get​data() function returns dictionary {} .

I guess the following lines are causing JSON encoder error

now = datetime.datetime.now()
now = datetime.datetime.strftime(now, '%Y-%m-%dT%H:%M:%S.%fZ')
print json.dumps({'current_time': now}) // I guess this is the culprit
6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I guess your string has a non ascii characters encoded in it.

I unable to decode with utf-8 it may happen if you need to use other encodings in your code. For e.g.:

>>> 'my weird character \x96'.decode('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x96 in position 19: invalid start byte

In your case, the encoding seems to be windows-1252 so you need to do as follows:

>>> 'my weird character \x96'.decode('windows-1252')
u'my weird character \u2013'

Now you have unicode so you can safely encode into utf-8.

9 7 4
38,600 points

Related questions

0 votes
2 answers 6.6K views
6.6K views
Problem : I have encountered the following error while compiling "process.py" python tools/process.py --input_dir data -- operation resize --outp ut_dir data2/resize data/0.jpg -> data2/resize/0.png Traceback (most recent call last): File "tools/process.py", line 235, in <module ... 0xff in position 0: invalid start byte What may be the cause of the error? I am using Python's version as 3.5.2.
asked Nov 22, 2019 peterlaw 6.9k points
0 votes
1 answer 502 views
502 views
Problem : Please find below my code for your reference. import os for root, dirs, files in os.walk('Path'):      for file in files:          if file.endswith('.c'):              with open(os.path.join(root, file)) as f:                     for line in f: ... already tried setting it with the open(os.path.join(root, file),'r',encoding='cp932') as f: but got the same above error
asked Jan 31, 2020 jwilliam 3.9k points
0 votes
1 answer 831 views
831 views
Problem : Getting bellow error while executing numpy arrays unicodedecodeerror: 'ascii' codec can't decode byte 0x90 in position 614: ordinal not in range(128)
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
2 answers 423 views
423 views
Problem : I want to read my .csv file into Python (Spyder) but I am facing the error. Please find below my code : import csv mydata = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener") mydata = csv.reader(mydata) print(mydata) I face the following error: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
asked Dec 6, 2019 alecxe 7.5k points
0 votes
1 answer 16 views
16 views
Problem: Hello Kodlogs, I need someone to help me out here. I keep getting an 'invalid byte sequence in utf-8' error while running a code in ruby.
asked Oct 28, 2020 Festus James 360 points
0 votes
1 answer 105 views
105 views
Problem: Recently I have joined the angular2 project. But now I am stuck with below listed errors. The errors occurred if I tried to send my JSON objects to my backend. It may be due a parsing of my JSON objects. I am very new to angualar so unable to find ... (core.umd.js:3971) at ZoneDelegate.invokeTask (zone.js:274) at Zone.runTask (zone.js:151) at XMLHttpRequest.ZoneTask.invoke (zone.js:345)
asked Aug 5, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 481 views
481 views
Problem : Currently I am facing some issues. I am trying to use C# with Json.NET. The problem is that I always get following error: {"Unexpected character encountered while parsing value: e. Path '', line 0, position 0."} So the way I amm using Json.NET is ... as follows: {"Unexpected character encountered while parsing value: e. Path '', line 0, position 0."} Does anyone have any solution on it?
asked Dec 9, 2019 alecxe 7.5k points
0 votes
1 answer 395 views
395 views
Problem : I am facing error &ldquo;Expecting value: line 1 column 1 (char 0)&rdquo; when trying to decode the JSON.The URL I use for a API call works fine in a browser, but gives above error when done through the curl request. The error happens at the return simplejson.loads(response_json)
asked Jan 20, 2020 jwilliam 3.9k points
0 votes
2 answers 7.1K views
7.1K views
Problem : I am trying to figure out the good way to load the JSON objects in the Python. I send below json data: {'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}} To my backend where it ... 1 column 2 (char 1)&rdquo; I did lot of research on it but nothing seems to work for me. Any suggestions on my issue will be much appreciated.
asked Jan 7, 2020 alecxe 7.5k points
0 votes
1 answer 10 views
10 views
Problem: I have a sample data, import pandas as pd df = { 'columA':['1A','ws rank','rank','ws rank','rank','Drank'], 'value': [ 1, 12, 34, 50, 3,2] } df = pd.DataFrame(df) 1. I need to make section "HP" for lines columnA which are "ws rank", " ... column that groups the "columN" values and sums the "HP" values, but that didn't work and returned null df2['HPpoint']=df2.groupby('columA')['HP'].sum()
asked Dec 30, 2020 sasha 5.3k points