Python 将列表转换为字符串并返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17796446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a list to a string and back
提问by Xandros
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?
是否有任何模块可以将字符串读入列表/以可读方式存储列表?
requirements:
要求:
- Must be human readable in stored form (hence "pickle" is not suitable)
- Must be relatively easy to implement
- 必须是人类可读的存储形式(因此“pickle”不合适)
- 必须相对容易实施
采纳答案by Slater Victoroff
回答by Martijn Pieters
Use the json
module:
使用json
模块:
string = json.dumps(lst)
lst = json.loads(string)
Demo:
演示:
>>> import json
>>> lst = [(0,4738),(0,36),
... (0,6376),(0,0)]
>>> string = json.dumps(lst)
>>> string
'[[0, 4738], [0, 36], [0, 6376], [0, 0]]'
>>> lst = json.loads(string)
>>> lst
[[0, 4738], [0, 36], [0, 6376], [0, 0]]
An alternative could be to use repr()
and ast.literal_eval()
; for just lists, tuples and integers that also allows you to round-trip:
另一种方法是使用repr()
and ast.literal_eval()
; 仅适用于还允许您往返的列表、元组和整数:
>>> from ast import literal_eval
>>> string = repr(lst)
>>> string
'[[0, 4738], [0, 36], [0, 6376], [0, 0]]'
>>> lst = literal_eval(string)
>>> lst
[[0, 4738], [0, 36], [0, 6376], [0, 0]]
JSON has the added advantage that it is a standard format, with support from tools outside of Python support serialising, parsing and validation. The json
library is also a lot faster than the ast.literal_eval()
function.
JSON 有一个额外的优势,它是一种标准格式,支持 Python 支持序列化、解析和验证之外的工具。该json
库也比快了很多ast.literal_eval()
功能。
回答by Sukrit Kalra
Just use ast.literal_eval
只需使用 ast.literal_eval
>>> from ast import literal_eval
>>> a = literal_eval('[(1, 2)]')
>>> a
[(1, 2)]
You can convert it into a string using repr()
.
您可以使用 将其转换为字符串repr()
。
>>> repr(a)
'[(1, 2)]'
回答by NPE
回答by inspectorG4dget
with open('path/to/file', 'w') as outfile:
for tup in L:
outfile.write("%s\n" %' '.join(str(i) for i in tup))
with open('path/to/file) as infile:
L = [tuple(int(i) for i in line.strip().split()) for line in infile]
回答by Sean McSomething
If you're just dealing with primitive Python types, you can just use the built-in repr()
:
如果您只是处理原始 Python 类型,则可以使用内置的repr()
:
Help on built-in function repr in module __builtin__:
repr(...)
repr(object) -> string
Return the canonical string representation of the object.
For most object types, eval(repr(object)) == object.
回答by malohm
eval
should do it in a simple way:
eval
应该以一种简单的方式做到这一点:
>>> str([(0,4738),(0,36),(0,6376),(0,0)])
'[(0, 4738), (0, 36), (0, 6376), (0, 0)]'
>>> eval(str([(0,4738),(0,36),(0,6376),(0,0)]))
[(0, 4738), (0, 36), (0, 6376), (0, 0)]