2010-08-04 4 views

Répondre

24

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

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

Je vois cela.

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 est une classe. Il gère Unicode. Il reflète la structure de bibliothèque Python 3 préférée.

StringIO.StringIO est une classe. Il gère les chaînes. Il reflète la structure de la bibliothèque Python 2 héritée.

What should be preferred?

Toujours aller de l'avant vers la nouvelle organisation de la bibliothèque. Le io.open doit être utilisé pour remplacer le Unicode-unaware intégré open.

Avant. Avance.

+4

En fait, aller de l'avant avec le comportement de io.StringIO peut causer des problèmes si vos autres paquets/modules 2.7 ne sont pas encore unicode. Au moins, il a fait pour moi quand gunicorn "avancé" et Django n'a pas [ici] (https://github.com/benoitc/gunicorn/pull/728/files) et [ici] (https: //code.djangoproject .com/ticket/20185). – hobs

+1

@hobs J'ai rencontré le même problème en essayant d'utiliser io.StringIO pour mock.patch 'argparse'. La meilleure façon de faire fonctionner les tests à la fois dans 2 et 3 était 'try: from StringIO import StringIO except ImportError: from io import StringIO'. – jtpereyda

Questions connexes