Embed pickle (or arbitrary) data in python script

前端 未结 4 1048
孤城傲影
孤城傲影 2021-01-13 10:08

In Perl, the interpreter kind of stops when it encounters a line with

__END__

in it. This is often used to embed arbitrary data at the end

4条回答
  •  梦毁少年i
    2021-01-13 10:25

    In Python, you can use """ (triple-quoted strings) to embed long runs of text data in your program.

    In your case, however, don't waste time on this.

    If you have an object you've pickled, you'd be much, much happier dumping that object as Python source and simply including the source.

    The repr function, applied to most objects, will emit a Python source-code version of the object. If you implement __repr__ for all of your custom classes, you can trivially dump your structure as Python source.

    If, on the other hand, your pickled structure started out as Python code, just leave it as Python code.

提交回复
热议问题