Skip to content

Commit

Permalink
doc: make it more clear that 'utf-8' encoding is expected.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Sep 12, 2020
1 parent 186c495 commit a86f8ed
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
6 changes: 4 additions & 2 deletions examples/json-to-nestedtext
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ from pathlib import Path
import json
import nestedtext
import sys
sys.stdin.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')

cmdline = docopt(__doc__)
input_filename = cmdline['<filename>']
Expand All @@ -32,7 +34,7 @@ try:
# read JSON content; from file or from stdin
if input_filename:
input_path = Path(input_filename)
json_content = input_path.read_text()
json_content = input_path.read_text(encoding='utf-8')
else:
json_content = sys.stdin.read()
data = json.loads(json_content)
Expand All @@ -46,7 +48,7 @@ try:
if output_path.exists():
if not cmdline['--force']:
fatal('file exists, use -f to force over-write.', culprit=output_path)
output_path.write_text(nestedtext_content)
output_path.write_text(nestedtext_content, encoding='utf-8')
else:
sys.stdout.write(nestedtext_content)
except OSError as e:
Expand Down
6 changes: 4 additions & 2 deletions examples/nestedtext-to-json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ from pathlib import Path
import json
import nestedtext
import sys
sys.stdin.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')


def de_dup(key, value, data, state):
Expand All @@ -35,7 +37,7 @@ on_dup = de_dup if cmdline['--dedup'] else None
try:
if input_filename:
input_path = Path(input_filename)
nestedtext_content = input_path.read_text()
nestedtext_content = input_path.read_text(encoding='utf-8')
else:
nestedtext_content = sys.stdin.read()
data = nestedtext.loads(nestedtext_content, input_filename, on_dup=de_dup)
Expand All @@ -45,7 +47,7 @@ try:
if output_path.exists():
if not cmdline['--force']:
fatal('file exists, use -f to force over-write.', culprit=output_path)
output_path.write_text(json_content)
output_path.write_text(json_content, encoding='utf-8')
else:
sys.stdout.write(json_content)
except OSError as e:
Expand Down
6 changes: 5 additions & 1 deletion nestedtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def loads(content, source=None, on_dup=None):
>>> filename = 'examples/duplicate-keys.nt'
>>> try:
... with open(filename) as f:
... with open(filename, encoding='utf-8') as f:
... addresses = nestedtext.loads(f.read(), filename)
... except nestedtext.NestedTextError as e:
... print(str(e))
Expand All @@ -535,6 +535,10 @@ def loads(content, source=None, on_dup=None):
6 «»
Notice in the above example the encoding is explicitly specified as
'utf-8'. *NestedText* files should always be read and written using
*utf-8* encoding.
The following examples demonstrate the various ways of handling
duplicate keys:
Expand Down
1 change: 0 additions & 1 deletion tests/nestedtext.py

This file was deleted.

0 comments on commit a86f8ed

Please sign in to comment.