2010-08-04 13 views

risposta

24

http://docs.python.org/library/io.html#io.StringIO

http://docs.python.org/library/stringio.html

vedo questo.

An in-memory stream for unicode text. It inherits TextIOWrapper.

This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files).

io.StringIO è una classe. Gestisce Unicode. Riflette la struttura della libreria Python 3 preferita.

StringIO.StringIO è una classe. Gestisce le stringhe. Riflette la struttura legacy della libreria Python 2.

What should be preferred?

Andare sempre avanti verso la nuova organizzazione della biblioteca. È necessario utilizzare io.open per sostituire Unicode-inconsapevole incorporato open.

Avanti. Andare avanti

+4

In realtà, il passaggio al comportamento io.StringIO può causare problemi se gli altri 2.7 pacchetti/moduli non sono ancora compatibili con Unicode. Almeno lo ha fatto per me quando gunicorn "si è mosso" e Django non ha [qui] (https://github.com/benoitc/gunicorn/pull/728/files) e [qui] (https: //code.djangoproject .com/biglietti/20185). – hobs

+1

@hobs Ho riscontrato lo stesso problema nel tentativo di utilizzare io.StringIO in mock.patch 'argparse'. Il modo migliore che ho trovato per far funzionare i test sia in 2 che in 3 era 'prova: da StringIO import StringIO tranne ImportError: da io import StringIO'. – jtpereyda

Problemi correlati