简体   繁体   中英

With Python linguini how do I open a file using codecs / gzip?

Normally I would open a utf-8 encoded file in Python like this:

import codecs
f = codecs.open('file_name', 'r', 'utf8')

How do I do this in a linguini ( https://github.com/enewe101/linguini ) Task using the File wrapper?

If you have a linguini File resource called my_file , you can use

path = my_file.get_path()
f = codecs.open(path, 'r', 'utf8)

Details : The linguini.File resource provides an open() method, which basically wraps the builtin open . That's there for convenience. The main raison d'etre of the File class is to transparently namespace your files -- helping you keep separate lots separate. You can take advantage of the namespacing, while using your own file-opening functionality by calling the File resource's get_path() method.

Here's a typical usage as would be done inside a Task :

from linguini import File, SimpleTask

class MyTask(SimpleTask):

    inputs = File('path/to/dir', 'file_name.ext')

    def run(self)
        fname = self.inputs.get_path()
        f = codecs.open(fname, 'r', 'utf8')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM