Python Forum
'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte
#10
Hi Buran and Snippsat,

Thanks for your advises and quick reply. Actually, I am using json module to working with json. I used json.dumps but it didn't work, the non-ascii isn't displayed correctly.

This is my previous code. I realized the json I got from Jira Rest API is correct but json.dumps() makes it incorrect.

def getTicketFieldsAndComments(TicketKeys, filepath):
    
    totalTickets = len(TicketKeys)
    f = Util.createFile(filepath)
    f.write('{ "total":' + str(totalTickets) + ', ')
    f.write('"smallest":"' + TicketKeys[0]  + '", ')
    f.write('"largest":"' + TicketKeys[totalTickets-1] + '", ')
    
    f.write('"issues":[')

    lastIssue =  totalTickets - 1
    for i, ticketNumber in enumerate(TicketKeys):
        
        issue = json.loads(subprocess.check_output('java -jar OAuthTutorialClient-1.0.jar request "https://impulsepoint.atlassian.net/rest/api/latest/issue/' + ticketNumber + '"' , shell=True, encoding="437"))
               
        if i == lastIssue:
            f.write(json.dumps(issue))             
        else:
            f.write(json.dumps(issue) + ',')    

    f.write(']}') 
   
    f.close()    
Cnull, I also tried to use ensure_ascii=False and it didn't work either

def getTicketFieldsAndComments(TicketKeys, filepath):
    
    totalTickets = len(TicketKeys)
    f = Util.createFile(filepath)
    f.write('{ "total":' + str(totalTickets) + ', ')
    f.write('"smallest":"' + TicketKeys[0]  + '", ')
    f.write('"largest":"' + TicketKeys[totalTickets-1] + '", ')
    
    f.write('"issues":[')

    lastIssue =  totalTickets - 1
    for i, ticketNumber in enumerate(TicketKeys):
        
        issue = json.loads(subprocess.check_output('java -jar OAuthTutorialClient-1.0.jar request "https://impulsepoint.atlassian.net/rest/api/latest/issue/' + ticketNumber + '"' , shell=True, encoding="437"))
               
        if i == lastIssue:            
            json_string = json.dumps(issue, ensure_ascii=False).encode('utf-8')
            f.write(json_string.decode())
           # f.write(json.dumps(issue))             
        else:
            json_string = json.dumps(issue, ensure_ascii=False).encode('utf-8')
            f.write(json_string.decode())
            f.write(",")
            #f.write(json.dumps(issue) + ',')    

    f.write(']}') 
   
    f.close()    
And this is the location in tmp.json which threw the exception and error
 "body": "Ashley Tarloski <[email protected]> commented:\n\nHi Dan,\n\nIxE2\u20acx99m on the phone with support now.\n\nThanks,\nAshley",
          "updated": "2017-08-03T13:58:41.682-0400"
The correct text should be

Ashley Tarloski <[email protected]> commented:\n\nHi Dan,\n\nI'm on the phone with support now.\n\nThanks,\nAshley
Regards,
Tien
Reply


Messages In This Thread
RE: 'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte - by tienttt - Sep-18-2020, 03:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,384 Aug-14-2023, 02:28 AM
Last Post: deanhystad
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,892 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 16: invalid cont Melcu54 3 5,031 Mar-26-2023, 12:12 PM
Last Post: Gribouillis
  Decode string ? JohnnyCoffee 1 833 Jan-11-2023, 12:29 AM
Last Post: bowlofred
  extract only text strip byte array Pir8Radio 7 3,003 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  [SOLVED] [Debian] UnicodeEncodeError: 'ascii' codec Winfried 1 1,037 Nov-16-2022, 11:41 AM
Last Post: Winfried
  sending byte in code? korenron 2 1,137 Oct-30-2022, 01:14 PM
Last Post: korenron
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 19,076 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  Byte Error when working with APIs Oshadha 2 1,023 Jul-05-2022, 05:23 AM
Last Post: deanhystad
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,746 Apr-29-2022, 12:36 PM
Last Post: Armandito

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020