Skip to content

Commit 98ead88

Browse files
committed
Handle unicode differently when running on Python 2
1 parent b48f5db commit 98ead88

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

omdbtool.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88
import json
99

1010
try:
11+
# Python 3
1112
from urllib.request import urlopen
1213
from urllib.parse import urlencode
14+
to_unicode = lambda s: s
15+
mk_trans = str.maketrans
1316
except ImportError:
14-
from urllib import urlopen, urlencode
17+
# Python 2
18+
from urllib2 import urlopen
19+
from urllib import urlencode
20+
to_unicode = lambda s: unicode(s) # noqa
21+
mk_trans = lambda a, b: {ord(ca): ord(cb) for ca, cb in zip(a, b)}
1522

1623

1724
parser = argparse.ArgumentParser(description='Get OMDb data for a movie')
@@ -135,17 +142,17 @@
135142

136143

137144
# known problematic characters to replace
138-
char_map = str.maketrans(
139-
'–',
140-
'-'
145+
char_map = mk_trans(
146+
u'–',
147+
u'-'
141148
)
142149

143150

144151
def fmt(s):
145152
# get rid of weird characters in output, which also cause errors on Windows
146153
# first use the preferred character mapping for known characters, then fall
147154
# back to encode + decode for unexpected ones
148-
return (s
155+
return (to_unicode(s)
149156
.translate(char_map)
150157
.encode('ascii', errors='replace')
151158
.decode('utf-8'))

0 commit comments

Comments
 (0)