如何将多个.txt文件腌制为一个腌制 [英] How to pickle several .txt files into one pickle

查看:108
本文介绍了如何将多个.txt文件腌制为一个腌制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要克服一些cPickle的限制, 也就是说,我需要打开多个文件并将它们腌制为一个文件,如下所示:

I need to overcome some cPickle constrains, namely i need to open several files and pickle them to one file, like this:

import cPickle

file1=open('file1.txt','r')
file2=open('file2.txt','r')
obj=[file1,file2] or obj=[file1.read(), file2.read()]
cPickle.dump(obj,open('result.i2','w'),2)

,以便稍后我可以重新选择"它们并获取数据.

so that later I can "repickle" them and get the data.

cPickle是这样做的好方法吗?如果是,我该如何正确地做到这一点

Is a cPickle good way to do so?If yes how can I do it properly

如果没有,什么合适?

预先感谢

Rafal

推荐答案

这是正确的方法,它将使文件内容腌制:

This is the correct way, it pickles the file contents:

file1=open('file1.txt','r')
file2=open('file2.txt','r')
obj=[file1.read(), file2.read()]
cPickle.dump(obj,open('result.i2','w'),2)

如果设置obj=[file1,file2],则不是在腌制文件内容,而是在腌制文件句柄.

If you set obj=[file1,file2] you are not pickling the file contents, you are pickling the file handles.

这篇关于如何将多个.txt文件腌制为一个腌制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆