0

I am using the pdfminer tool to convert pdf to .csv (text) and one of the subcommands in the tool pdfinterp.py still uses the CStringIO and StringIO for string to string translation -

import re
try:
    from CStringIO import StringIO 
except ImportError:
    from StringIO import StringIO

I am using Python 3 so I am aware of the need to change to io and io.StringIO.

How exactly should the above command be re-worded in pdfinterp to make it functional in Python 3.

jwpfox
  • 5,124
  • 11
  • 45
  • 42

1 Answers1

0

you could extend your import block to make it compatible with all versions (Python 2.x or 3.x). Ugly because of all try/except blocks but would work

try:
    from CStringIO import StringIO 
except ImportError:
    try:
        from StringIO import StringIO
    except ImportError:
        from io import StringIO

or (slightly better)

import sys
if sys.version_info < (3,)
   try:
       from CStringIO import StringIO 
   except ImportError:        
        from StringIO import StringIO
else:
     from io import StringIO

Be aware that python 3 has BytesIO too because binary data and text data is different now. So if StringIO is used to pass binary data it will fail.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219