将列表转换为字符串并返回 [英] Convert a list to a string and back

查看:57
本文介绍了将列表转换为字符串并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个虚拟机,它从嵌套在列表中的元组中读取指令,如下所示:

I have a virtual machine which reads instructions from tuples nested within a list like so:

[(0,4738),(0,36),
 (0,6376),(0,0)]

存储这种机器码程序时,文本文件最简单,必须写成字符串.这显然很难转换回来.

When storing this kind of machine code program, a text file is easiest, and has to be written as a string. Which is obviously quite hard to convert back.

是否有任何模块可以将字符串读入列表/以可读方式存储列表?

Is there any module which can read a string into a list/store the list in a readable way?

要求:

  • 必须是人类可读的存储形式(因此pickle"不合适)
  • 必须相对容易实施

推荐答案

JSON!

import json

with open(data_file, 'wb') as dump:
    dump.write(json.dumps(arbitrary_data))

类似:

source = open(data_file, 'rb').read()
data = json.loads(source)

这篇关于将列表转换为字符串并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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