python里的变量值如何存储到txt文件中

import sys
if sys.version_info[0] == 2:
import cPickle as pickle
else:
import pickle

fo = open('train_data.pkl', 'rb')
if sys.version_info[0] == 2:
entry = pickle.load(fo)
else:
entry = pickle.load(fo, encoding='latin1')
fo.close()

train_data2 = entry['train_data2']
train_data1 = entry['train_data1']
train_labels1 = entry['train_labels1']
valid_data1 = entry['valid_data1']
valid_labels1 = entry['valid_labels1']
valid_data2 = entry['valid_data2']
valid_labels2 = entry['valid_labels2']

f = open('E:\1.txt','w')
f.write(train_data1)

我从pkl文件里读出来的变量不知道怎么存到txt里,打开生成的txt一直是空的。求解答!

2个回答

最后加个close试试

最好这样使用 with open('E:\1.txt','w') as fs: fs.write(str(x))

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐