Luigi LocalTarget binary file
Asked Answered
I

2

15

I am having troubles to write a binary LocalTarget in a Luigi pipeline in my project. I isolated the problem here:

class LuigiTest(luigi.Task):
    def output(self):
        return luigi.LocalTarget('test.npz')

    def run(self):
        with self.output().open('wb') as fout:
            np.savez_compressed(fout, array=np.asarray([1, 2, 3]))

I tried opening as 'w' and 'wb' but I keep getting the following error:

TypeError: write() argument must be str, not bytes

I am using python 3.5.1 and my version of luigi is 2.1.1

Instead answered 27/8, 2016 at 9:33 Comment(0)
I
16

The problem was with the format of the LocalTarget. Changing it to:

return luigi.LocalTarget('test.npz', format=luigi.format.Nop)

solved the problem. However there was nothing about this in the documentation.

Instead answered 27/8, 2016 at 20:41 Comment(1)
Same problem here, very useful!Leonaleonanie
A
3

It solved my problem with writing a parquet file in Hadoop. format=luigi.format.Nop did the trick. Thanks!

import luigi
import pandas as pd
import luigi.contrib.hdfs as hdfs

class Hdfs(luigi.Task):
    """
    Writes files into output.
    """
    def __init__(self, *args, **kwargs):
        super(Hdfs, self).__init__( *args, **kwargs)

    def output(self):
        fname_template = f'/data/some_directory/test_luigi.parq'
        return luigi.contrib.hdfs.HdfsTarget(fname_template, format=luigi.format.Nop)

    def run(self):
        with self.output().open('w') as f:
            print(f.path)
            d = pd.DataFrame({'sim_id':[1,2,3]})
            d.to_parquet(f)
Anisole answered 10/3, 2019 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.