简体   繁体   中英

Python Type Error While Writing a json File

I am making a dictionary of us metropolitan areas. Structure is like this:

  {
  "Abilene": {
    "allCities": [
      "Abilene"
    ], 
    "latitude": "30.1588129", 
    "longName": "Abilene, TX Metropolitan Statistical Area", 
    "longitude": "-85.6602058", 
    "primaryState": "TX", 
    "states": [
      "TX"
    ]
  }, 

I want to write this dictionary to a file as json encoded in unicode, so I imported unicode literals and the io package:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import io
from os.path import exists
import json
import unicodedata

I am setting keys in literals and values in unicode strings.

metropolitans[primaryCity] = {'allCities':allCities, 'longName':longName, 'allStates':states, 'primaryState':states[0]}

metropolitans[primaryCity]['longitude'] = unicode(str(coordinates[primaryCity]['longitude']), encoding = 'utf-8')

Then, I want to save metropolitans to a json file in unicode.

with io.open('results.json', 'w', encoding='utf-8') as results:
results.truncate()
json.dump(metropolitans, results, ensure_ascii=False, encoding='utf-8', results, indent=2, sort_keys=True)

And, I get this error:

File "readMetropolitan.py", line 190, in <module>
json.dump(metropolitans, results, indent=2, sort_keys=True)
File "/usr/lib/python2.7/json/__init__.py", line 190, in dump
    fp.write(chunk)
TypeError: write() argument 1 must be unicode, not str

Why is that? I know that I can complete writing a file with 'wb' but I want to write this file in unicode.

I can write this file if I use dumps() and write():

s = json.dumps(metropolitans, ensure_ascii=False, encoding='utf-8', indent=2, sort_keys=True)
results.write(s)

The code you posted above the error code hasn't even been ran since the error traceback said the error occurred when running this line at line 190 in file readMetropolitan.py:

json.dump(metropolitans, results, indent=2, sort_keys=True)

While the code you posted was:

json.dump(metropolitans, ensure_ascii=False, encoding='utf-8', results, indent=2, sort_keys=True)

They are definitely not the same. Perhaps the code was ran before the edit was made?

The first two lines of the error traceback specifically stated what triggered the error.

File "readMetropolitan.py", line 190, in <module>
json.dump(metropolitans, results, indent=2, sort_keys=True)
File "/usr/lib/python2.7/json/__init__.py", line 190, in dump
    fp.write(chunk)
TypeError: write() argument 1 must be unicode, not str

Plus, you cannot have a positional argument results after keyword arguments. Therefore, it should instead be:

json.dump(metropolitans, results, ensure_ascii=False, encoding='utf-8', indent=2, sort_keys=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM