0

I'm trying to convert the file "Ano-2016.json" to csv format, but my program returns the following error: UnicodeDecodeError: 'utf8' codec can not decode byte 0xc7 in position 7: invalid continuation byte.

Can someone please help me? This is my code:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import csv
import unicodecsv as csv
import json


infile = open("Ano-2016.json", "r") 
outfile = open("Ano2016_converted.csv", "w")

writer = csv.writer(outfile)

for row in json.loads(infile.read()):
    writer.writerow(row)
  • Try `writer.writerow(row.encode('utf-8'))`. It's possibly one of them is unicode. – Zizouz212 Jul 16 '17 at 20:02
  • @Zizouz212 after the change that you suggested the same error still persists. – França Jul 16 '17 at 20:07
  • Are you sure `Ano-2016.json` has `utf-8` encoding? You may need to specify its correct encoding via the `encoding` parameter on `open`. Take a look at this: https://stackoverflow.com/questions/19699367/unicodedecodeerror-utf-8-codec-cant-decode-byte. – peterfields Jul 16 '17 at 20:09
  • You're hitting the error when you're trying to write the row, right? Also, your first csv import is pointless. Didn't see the unicode part, but it's probably that your json file isn't using a utf-8 encoding, so it's screwing you over when converting to unicode. – Zizouz212 Jul 16 '17 at 20:10
  • 1
    You should mention which Python version you're using (preferably with the appropriate tag), and it would help if you added a small sample of the JSON data. Also, when posting error messages it's a good idea to post the full traceback, or at least clearly indicate which line generated the error. – PM 2Ring Jul 16 '17 at 20:26
  • Thanks for all the advice, I'm new here. I tried to specify encoding parameter on "open", but I had the following error "'encoding' is an invalid keyword argument for this function". So I decided to use "io.open", and my computer crashed. The error is in the row: "for row in json.loads (infile.read ()):" – França Jul 16 '17 at 20:51
  • link for download the data: [link](http://www.camara.leg.br/cotas/Ano-2016.json.zip) – França Jul 16 '17 at 21:00

0 Answers0