0

I'm using Python 2.7.14 .

When execute this,

# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import os
import json

def main():
    path = os.path.join(os.path.dirname(__file__), 'data-creation.json')
    with open(path, 'r+') as f:
        json_data = json.load(f)
        json.dump(json_data, path)

if __name__=='__main__':
    main()

it says AttributeError: 'unicode' object has no attribute 'write' in line of json.dump(json_data, path) .

nkf -g data-creation.json outputs ASCII, but what is wrong? I think this is a matter of char code.

1 Answer 1

1

json.dump expects a file-like object which supports write() as second parameter, not a path.

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.