0

Code:

with open(read_json,'r',encoding='utf-8') as json_file:

    json_data = json.load(json_file)
    print(json_data)
    with open(write_csv,'w',encoding='utf-8') as csv_file:

        headers, items = parse_json(json_data,query_type)
        # i is to be iterated to get all maxResults = 50.
        writer = csv.writer(csv_file)
        writer.writerow(headers)
        for row in items:
            writer.writerow(row)

CSV file:

enter image description here enter image description here

Im having weird characters in my CSV files not exactly sure whats happening.

3
  • That is UTF-8 multibyte characters displayed as the wrong code page, likely cp1252 if on Windows. Are you viewing the CSV in Excel or Notepad? Use utf-8-sig as the encoding and it should fix the issue. Commented Nov 29, 2017 at 21:45
  • @MarkTolonen im using excel Commented Nov 29, 2017 at 21:47
  • @MarkTolonen it worked thank you so much, can you explain what went wrong, im new to programming. Commented Nov 29, 2017 at 21:50

1 Answer 1

1

Windows applications often assume text files are encoded in an ANSI encoding, which varies by localized Windows version. Windows-1252 is the encoding used by U.S. Windows.

Excel also makes this assumption, but if it sees a UTF8 BOM signature, it will use UTF8 for the encoding. Use the file encoding utf-8-sig to write this signature. If used to open a file for reading, it recognizes and discards the signature so it doesn't affect your Python code.

So use utf-8-sig instead to give Excel the hint it needs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.