Giter VIP home page Giter VIP logo

Comments (2)

brennanyoung avatar brennanyoung commented on September 24, 2024

This is a python 2 vs 3 issue. You mention that you are using Python 3.4.1. This library is made for Python 2, but it is very easy to fix. (I hope toutpt will read this and post an update).

Python 3 always uses unicode for the str type, and the str type no longer has (or needs) a decode method. Reading a str from a file in python 3 will always give you unicode, which needs no further decoding. If you try to call str.decode() in python 3 you will get this error.

A quick-and-dirty solution to this is to change core.py in this library, so that decode only gets called if it exists (which is effectively the same as checking whether you are running python 2). You could also use a try block, to catch that specific exception.

The decode method is called in two places in core.py.

I'm providing some code compatible with both python2+3 that you can use:

...in the read() method of the Reader class.

    def read(self):
        self.rawcontent = self.fileobject.read()        
        if type(self.rawcontent) == str:
            if hasattr(self.rawcontent, "decode"):
                self.rawcontent = self.rawcontent.decode(self.encoding)            
        self.text_to_captions()
        return self.captions

...and in the set_text() method of the Caption class

    def set_text(self, value):
        if type(value) == str:
            if hasattr(str, "decode"):
                value = str.decode(self.encoding)
        elif type(value) != unicode:
            raise ValueError("text must be either encoded string or unicode")
        self._text = value

This isn't a completely bulletproof solution, but in the absence of malicious code, it should 'just work'. I am sure better solutions can be imagined, but in my case, I just want to get it working.

BTW: Switching the decode issue will make the captionstransformer library compatible with python 2 & 3.

from captionstransformer.

toutpt avatar toutpt commented on September 24, 2024

closed by PR #2

from captionstransformer.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.