File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88import json
99
1010try :
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
1316except 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
1724parser = argparse .ArgumentParser (description = 'Get OMDb data for a movie' )
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
144151def 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' ))
You can’t perform that action at this time.
0 commit comments