user4517908
user4517908

Reputation: 35

Gzip and Encode file in Python 2.7

with gzip.open(sys.argv[5] + ".json.gz", mode="w", encoding="utf-8") as outfile:

It throws:

TypeError: open() got an unexpected keyword argument 'encoding'

But the docs says it exists https://docs.python.org/3/library/gzip.html

Update

How can i encode and zip the file in Python 2.7?

I tried now this: (but it don't work)

with gzip.open(sys.argv[5] + ".json.gz", mode="w") as outfile:
    outfile = io.TextIOWrapper(outfile, encoding="utf-8")
    json.dump(fdata, outfile, indent=2, ensure_ascii=False)

TypeError: must be unicode, not str

What can i do?

Upvotes: 2

Views: 2505

Answers (2)

Brian Schirmacher
Brian Schirmacher

Reputation: 11

Seems the question has been answered sufficiently, but for your peace of mind: Alternatively to ensure that Python2 uses utf-8 as standard perhaps try the following, as it then becomes unnecessary to specify an encoding:

import sys
reload(sys)
sys.setdefaultencoding('UTF8')

Upvotes: 1

senshin
senshin

Reputation: 10360

Those are the Python 3 docs. The Python 2 version of gzip does not allow encoding= as a keyword argument to gzip.open().

Upvotes: 2

Related Questions