1

I have a json settings file that contains the settings for my script. Part of the settings is the delimiter for the csv.writer().

When it is being read into the variable settings and printed it giving me this: {'delimiter': u','}

When I try use this in the csv.writer(out, **settings) I get the following error: TypeError: "delimiter" must be an 1-character string.

Now to fix this I do do this: settings["delimiter"] = settings["delimiter"].encode("utf-8")

...but I am wondering if there is a way I can read this file in the first place which would mean I didn't have do to this?

EDIT:

Settings file is read like this:

    with open(file) as settings:
        details = json.load(settings)
Cheetah
  • 13,785
  • 31
  • 106
  • 190

1 Answers1

1

No, the JSON standard uses unicode throughout (this is a good thing). The json library has no options, short of subclassing the decoder, to switch to returning byte strings instead.

Just encode the delimiter.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343