我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用encodings.ascii()。
def read_string(conn): """ reads length of text to read, and then the text encoded in UTF-8, and returns the string""" strlen = read_int(conn) if not strlen: return '' res = to_bytes('') while len(res) < strlen: res = res + conn.recv(strlen - len(res)) res = utf_8.decode(res)[0] if sys.version_info[0] == 2 and sys.platform != 'cli': # Py 2.x, we want an ASCII string if possible try: res = ascii.Codec.encode(res)[0] except UnicodeEncodeError: pass return res
def __init__(self, stream, fieldnames, encoding='utf-8', **kwds): """Initialzer. Args: stream: Stream to write to. fieldnames: Fieldnames to pass to the DictWriter. encoding: Desired encoding. kwds: Additional arguments to pass to the DictWriter. """ writer = codecs.getwriter(encoding) if (writer is encodings.utf_8.StreamWriter or writer is encodings.ascii.StreamWriter or writer is encodings.latin_1.StreamWriter or writer is encodings.cp1252.StreamWriter): self.no_recoding = True self.encoder = codecs.getencoder(encoding) self.writer = csv.DictWriter(stream, fieldnames, **kwds) else: self.no_recoding = False self.encoder = codecs.getencoder('utf-8') self.queue = cStringIO.StringIO() self.writer = csv.DictWriter(self.queue, fieldnames, **kwds) self.stream = writer(stream)
def to_bytes(cmd_str): return ascii.Codec.encode(cmd_str)[0]
def source_to_unicode(txt, errors='replace', skip_encoding_cookie=True): """Converts a bytes string with python source code to unicode. Unicode strings are passed through unchanged. Byte strings are checked for the python source file encoding cookie to determine encoding. txt can be either a bytes buffer or a string containing the source code. """ if isinstance(txt, six.text_type): return txt if isinstance(txt, six.binary_type): buffer = io.BytesIO(txt) else: buffer = txt try: encoding, _ = detect_encoding(buffer.readline) except SyntaxError: encoding = "ascii" buffer.seek(0) newline_decoder = io.IncrementalNewlineDecoder(None, True) text = io.TextIOWrapper(buffer, encoding, errors=errors, line_buffering=True) text.mode = 'r' if skip_encoding_cookie: return u"".join(strip_encoding_cookie(text)) else: return text.read()