Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • This will also fail for older versions of python Commented Apr 20, 2015 at 16:21
  • @PadraicCunningham what is the minimum Python version this works for ? Commented Apr 20, 2015 at 16:25
  • 4
    The python community has adopted the EAFP (easier to ask forgiveness than permission) philosophy, so actually the try/except is more pythonic than this. Commented May 22, 2019 at 2:19
  • 1
    @cowlinator You are correct if you can catch a specific exception. Catching a general exception is bad practice. Commented May 22, 2019 at 12:25
  • 1
    Keep in mind this will fail in Python 2 if data has non-ascii characters. Commented Jan 4, 2022 at 19:59