我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sys.setdefaultencoding()。
def setencoding(): """Set the string encoding used by the Unicode implementation. The default is 'ascii', but if you're willing to experiment, you can change this.""" encoding = "ascii" # Default value set by _PyUnicode_Init() if 0: # Enable to support locale aware default string encodings. import locale loc = locale.getdefaultlocale() if loc[1]: encoding = loc[1] if 0: # Enable to switch off string to Unicode coercion and implicit # Unicode to string conversion. encoding = "undefined" if encoding != "ascii": # On Non-Unicode builds this will raise an AttributeError... sys.setdefaultencoding(encoding) # Needs Python Unicode build !
def run(): ''' ???? ''' reload(sys) sys.setdefaultencoding('utf8') program = os.path.basename(sys.argv[0]) logger = logging.getLogger(program) logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s') logging.root.setLevel(level=logging.INFO) logger.info("running %s" % ' '.join(sys.argv)) outp1 = r'wiki_model' outp2 = r'vector.txt' model = Word2Vec(sentences, size=400, window=5, min_count=5, workers=multiprocessing.cpu_count()) model.save(outp1) model.wv.save_word2vec_format(outp2, binary=False) testData = ['??','??','??','??'] for i in testData: temp = model.most_similar(i) for j in temp: print '%f %s'%(j[1],j[0]) print ''
def main(): global ENABLE_USER_SITE abs__file__() known_paths = removeduppaths() if ENABLE_USER_SITE is None: ENABLE_USER_SITE = check_enableusersite() known_paths = addusersitepackages(known_paths) known_paths = addsitepackages(known_paths) if sys.platform == 'os2emx': setBEGINLIBPATH() setquit() setcopyright() sethelper() aliasmbcs() setencoding() execsitecustomize() if ENABLE_USER_SITE: execusercustomize() # Remove sys.setdefaultencoding() so that users cannot change the # encoding after initialization. The test for presence is needed when # this module is run as a script, because this code is executed twice. if hasattr(sys, "setdefaultencoding"): del sys.setdefaultencoding
def main(): import sys reload(sys) sys.setdefaultencoding("utf-8") argparser = argparse.ArgumentParser() argparser.add_argument('--model', type=str) argparser.add_argument('--test_file', type=str) argparser.add_argument('--cuda', action='store_true') args = argparser.parse_args() model = torch.load(args.model) print(model.vocab_size) batch_size = 1000 tester = Tester(args.test_file, batch_size, model.mapping) perplexity = tester.calc_perplexity(model, cuda=args.cuda) print("Test File: {}, Perplexity:{}".format(args.test_file, perplexity))
def getContent2FileWithoutProcess(fileName = '',targetUrl = ''): reload(sys) sys.setdefaultencoding('utf-8') if fileName == '' or targetUrl == '': print 'params lost' else : #cookies = cookielib.MozillaCookieJar('cookies.txt') content = requests.get(targetUrl) #print content.text #str = extractData(content=content.text,type=type) #???????,??????????????CPU?? dataFile = open(fileName, 'w') #print content.encoding content.encoding = 'utf-8' #print content.encoding temp = content.text dataFile.write(temp) dataFile.close() print '%s json file saved' % fileName
def main(): sys.frozen = 'windows_exe' sys.setdefaultencoding('utf-8') aliasmbcs() sys.meta_path.insert(0, PydImporter()) sys.path_importer_cache.clear() import linecache def fake_getline(filename, lineno, module_globals=None): return '' linecache.orig_getline = linecache.getline linecache.getline = fake_getline abs__file__() add_calibre_vars() # Needed for pywintypes to be able to load its DLL sys.path.append(os.path.join(sys.app_dir, 'app', 'DLLs')) return run_entry_point()
def set_default_encoding(): try: locale.setlocale(locale.LC_ALL, '') except: print ('WARNING: Failed to set default libc locale, using en_US.UTF-8') locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') try: enc = locale.getdefaultlocale()[1] except Exception: enc = None if not enc: enc = locale.nl_langinfo(locale.CODESET) if not enc or enc.lower() == 'ascii': enc = 'UTF-8' try: enc = codecs.lookup(enc).name except LookupError: enc = 'UTF-8' sys.setdefaultencoding(enc) del sys.setdefaultencoding
def __init__(self, session): cmd.Cmd.__init__(self) self.session = session self.prompt = green('$ ') # Load all available modules self._load_modules() # Load history file self._load_history() # Set a nice intro self.intro = messages.welcome(self.session) # Set default encoding utf8 reload(sys) sys.setdefaultencoding('utf8')