请教一个问题,我把一个二维数组保存到本地,然后另外一段程序从本地再读取出来,赋给一个新变量,可是变量在输出的时候却变成了字符串的形式

0
已邀请:
1

牟瑞 - 大数据 Hadoop 讲师 Hadoop入门课程地址:http://www.hellobi.com/course/39 有架构师,技术总监,CTO的职位请联系我! 2016-08-09 回答

既然是保存到本地了,肯定是以字符串或者序列化的方式保存啊。
两种方式:
1.重新解析这个字符串,还原回二维数组
2.通过序列化和反序列化的方式,将变量的值还原回去
#!/usr/bin/python  
# Filename: pickling.py

import cPickle as p
#import pickle as p

shoplistfile = 'shoplist.data'
# the name of the file where we will store the object

shoplist = ['apple', 'mango', 'carrot']

# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()

del shoplist # remove the shoplist

# Read back from the storage
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist

要回复问题请先登录注册