简体   繁体   English

使用Python linguini,如何使用编解码器/ gzip打开文件?

[英]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: 通常,我会这样在Python中打开utf-8编码的文件:

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? 如何使用文件包装程序在linguini( https://github.com/enewe101/linguini )任务中执行此操作?

If you have a linguini File resource called my_file , you can use 如果您有一个名为my_file File资源,则可以使用

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 . 详细信息linguini.File资源提供了一个open()方法,该方法基本上包装了内置的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. File类的主要存在目的是为File透明地命名空间-帮助您将各个批次分开。 You can take advantage of the namespacing, while using your own file-opening functionality by calling the File resource's get_path() method. 您可以通过调用File资源的get_path()方法来使用命名空间,同时使用自己的文件打开功能。

Here's a typical usage as would be done inside a Task : 这是在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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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