baidu_31302703
baidu_31302703
采纳率0%
2017-06-07 14:36 阅读 3.1k

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条回答 默认 最新

  • gracewyc gracewyc 2017-06-08 01:56

    最后加个close试试

    点赞 1 评论 复制链接分享
  • qq948993066 TensorGroot 2017-06-08 06:15

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

    点赞 评论 复制链接分享

相关推荐