如何将列表完全按照python中的显示方式写入文本文件?

如何将列表完全按照python中的显示方式写入文本文件?,python,Python,我希望能够将列表写入文本文件,就像在控制台中打印出来一样(带括号和所有内容)。原因是,我以后需要使用该列表,它必须像打印时一样显示 我尝试过使用csv模块,但无法按照我的要求对其进行格式化 有什么建议吗?您可以创建一个小的帮助函数来表示列表的字符串 def listify(l): return '[{}]'.format(', '.join(map(str,l))) >>> l = [1,2,3,4,5] >>> listify(l) '[1, 2,

我希望能够将列表写入文本文件,就像在控制台中打印出来一样(带括号和所有内容)。原因是,我以后需要使用该列表,它必须像打印时一样显示

我尝试过使用csv模块,但无法按照我的要求对其进行格式化


有什么建议吗?

您可以创建一个小的帮助函数来表示列表的字符串

def listify(l):
    return '[{}]'.format(', '.join(map(str,l)))

>>> l = [1,2,3,4,5]
>>> listify(l)
'[1, 2, 3, 4, 5]'

您可以创建一个小的辅助函数来表示列表的字符串

def listify(l):
    return '[{}]'.format(', '.join(map(str,l)))

>>> l = [1,2,3,4,5]
>>> listify(l)
'[1, 2, 3, 4, 5]'

您可以创建一个小的辅助函数来表示列表的字符串

def listify(l):
    return '[{}]'.format(', '.join(map(str,l)))

>>> l = [1,2,3,4,5]
>>> listify(l)
'[1, 2, 3, 4, 5]'

您可以创建一个小的辅助函数来表示列表的字符串

def listify(l):
    return '[{}]'.format(', '.join(map(str,l)))

>>> l = [1,2,3,4,5]
>>> listify(l)
'[1, 2, 3, 4, 5]'

尝试使用
repr
。repr“返回对象的规范字符串表示形式”,因此它应该为您提供与在交互式提示上检查内容时打印的字符串相同的字符串

file.write(repr(my_list))

尝试使用
repr
。repr“返回对象的规范字符串表示形式”,因此它应该为您提供与在交互式提示上检查内容时打印的字符串相同的字符串

file.write(repr(my_list))

尝试使用
repr
。repr“返回对象的规范字符串表示形式”,因此它应该为您提供与在交互式提示上检查内容时打印的字符串相同的字符串

file.write(repr(my_list))

尝试使用
repr
。repr“返回对象的规范字符串表示形式”,因此它应该为您提供与在交互式提示上检查内容时打印的字符串相同的字符串

file.write(repr(my_list))

在写入之前使用str:

>>> a = [1,2,3,4]
>>> b = str(a)
>>> b
'[1, 2, 3, 4]'
>>> file_descriptor.write(b)

在写入之前使用str:

>>> a = [1,2,3,4]
>>> b = str(a)
>>> b
'[1, 2, 3, 4]'
>>> file_descriptor.write(b)

在写入之前使用str:

>>> a = [1,2,3,4]
>>> b = str(a)
>>> b
'[1, 2, 3, 4]'
>>> file_descriptor.write(b)

在写入之前使用str:

>>> a = [1,2,3,4]
>>> b = str(a)
>>> b
'[1, 2, 3, 4]'
>>> file_descriptor.write(b)

您可以使用
str
方法来包装字符串

In [21]: l = [1,2,3]

In [22]: with open("file_test", 'a') as f:
   ....:     f.write(str(l) + "\n")
   ....:     

In [23]: 

您可以使用
str
方法来包装字符串

In [21]: l = [1,2,3]

In [22]: with open("file_test", 'a') as f:
   ....:     f.write(str(l) + "\n")
   ....:     

In [23]: 

您可以使用
str
方法来包装字符串

In [21]: l = [1,2,3]

In [22]: with open("file_test", 'a') as f:
   ....:     f.write(str(l) + "\n")
   ....:     

In [23]: 

您可以使用
str
方法来包装字符串

In [21]: l = [1,2,3]

In [22]: with open("file_test", 'a') as f:
   ....:     f.write(str(l) + "\n")
   ....:     

In [23]: 

如果您正在寻找更通用的解决方案,您可能希望使用json:

import json
my_list = ['hello','world']
print json.dumps(my_list, indent=4, sort_keys=True, separators=(',', ': '))
输出为:

[
    "hello",
    "world"
]
{
    "hello": 1,
    "my_list": [
        "hello",
        "world"
    ],
    "world": 2
}
它不仅仅适用于列表:

my_dict = {
    'hello':1,
    'world':2
}
my_dict['my_list'] = my_list
print json.dumps(my_dict, indent=4, sort_keys=True, separators=(',', ': '))
输出为:

[
    "hello",
    "world"
]
{
    "hello": 1,
    "my_list": [
        "hello",
        "world"
    ],
    "world": 2
}
很好的一点是,您可以使用“json.loads”将字符串更改回原始值。下面是一个示例,您可以将字符串保存到一个文件中,然后读入以重新创建字典:

with open(r'c:\my_dict.json', 'w') as f:
    f.write(json.dumps(my_dict, indent=4, sort_keys=True, separators=(',', ': ')))
with open(r'c:\my_dict.json', 'r') as f:
    data = f.read()
    my_loaded_dict = json.loads(data)
print my_loaded_dict['my_list']
print my_loaded_dict.keys()
输出为(请注意,字符串现在是unicode):


如果您正在寻找更通用的解决方案,您可能希望使用json:

import json
my_list = ['hello','world']
print json.dumps(my_list, indent=4, sort_keys=True, separators=(',', ': '))
输出为:

[
    "hello",
    "world"
]
{
    "hello": 1,
    "my_list": [
        "hello",
        "world"
    ],
    "world": 2
}
它不仅仅适用于列表:

my_dict = {
    'hello':1,
    'world':2
}
my_dict['my_list'] = my_list
print json.dumps(my_dict, indent=4, sort_keys=True, separators=(',', ': '))
输出为:

[
    "hello",
    "world"
]
{
    "hello": 1,
    "my_list": [
        "hello",
        "world"
    ],
    "world": 2
}
很好的一点是,您可以使用“json.loads”将字符串更改回原始值。下面是一个示例,您可以将字符串保存到一个文件中,然后读入以重新创建字典:

with open(r'c:\my_dict.json', 'w') as f:
    f.write(json.dumps(my_dict, indent=4, sort_keys=True, separators=(',', ': ')))
with open(r'c:\my_dict.json', 'r') as f:
    data = f.read()
    my_loaded_dict = json.loads(data)
print my_loaded_dict['my_list']
print my_loaded_dict.keys()
输出为(请注意,字符串现在是unicode):


如果您正在寻找更通用的解决方案,您可能希望使用json:

import json
my_list = ['hello','world']
print json.dumps(my_list, indent=4, sort_keys=True, separators=(',', ': '))
输出为:

[
    "hello",
    "world"
]
{
    "hello": 1,
    "my_list": [
        "hello",
        "world"
    ],
    "world": 2
}
它不仅仅适用于列表:

my_dict = {
    'hello':1,
    'world':2
}
my_dict['my_list'] = my_list
print json.dumps(my_dict, indent=4, sort_keys=True, separators=(',', ': '))
输出为:

[
    "hello",
    "world"
]
{
    "hello": 1,
    "my_list": [
        "hello",
        "world"
    ],
    "world": 2
}
很好的一点是,您可以使用“json.loads”将字符串更改回原始值。下面是一个示例,您可以将字符串保存到一个文件中,然后读入以重新创建字典:

with open(r'c:\my_dict.json', 'w') as f:
    f.write(json.dumps(my_dict, indent=4, sort_keys=True, separators=(',', ': ')))
with open(r'c:\my_dict.json', 'r') as f:
    data = f.read()
    my_loaded_dict = json.loads(data)
print my_loaded_dict['my_list']
print my_loaded_dict.keys()
输出为(请注意,字符串现在是unicode):


如果您正在寻找更通用的解决方案,您可能希望使用json:

import json
my_list = ['hello','world']
print json.dumps(my_list, indent=4, sort_keys=True, separators=(',', ': '))
输出为:

[
    "hello",
    "world"
]
{
    "hello": 1,
    "my_list": [
        "hello",
        "world"
    ],
    "world": 2
}
它不仅仅适用于列表:

my_dict = {
    'hello':1,
    'world':2
}
my_dict['my_list'] = my_list
print json.dumps(my_dict, indent=4, sort_keys=True, separators=(',', ': '))
输出为:

[
    "hello",
    "world"
]
{
    "hello": 1,
    "my_list": [
        "hello",
        "world"
    ],
    "world": 2
}
很好的一点是,您可以使用“json.loads”将字符串更改回原始值。下面是一个示例,您可以将字符串保存到一个文件中,然后读入以重新创建字典:

with open(r'c:\my_dict.json', 'w') as f:
    f.write(json.dumps(my_dict, indent=4, sort_keys=True, separators=(',', ': ')))
with open(r'c:\my_dict.json', 'r') as f:
    data = f.read()
    my_loaded_dict = json.loads(data)
print my_loaded_dict['my_list']
print my_loaded_dict.keys()
输出为(请注意,字符串现在是unicode):



str
。对于列表,他们会做同样的事情。这是真的。不过,OP应该注意非列表对象。例如,如果他想在文件中写入字符串、引号和所有内容,
repr
str
更可取。是的,对于其他类型,这完全取决于你想要什么
print
在传递任意对象时使用
str
,而交互式解释器的REPL使用
repr
@Kevin非常感谢您的帮助!使用“repr”完全符合我的要求。我将在允许的情况下尽快标记为已回答。我需要如何设置格式,以便列表的每个索引显示在不同的行上?或
str
。对于列表,他们会做同样的事情。这是真的。不过,OP应该注意非列表对象。例如,如果他想在文件中写入字符串、引号和所有内容,
repr
str
更可取。是的,对于其他类型,这完全取决于你想要什么
print
在传递任意对象时使用
str
,而交互式解释器的REPL使用
repr
@Kevin非常感谢您的帮助!使用“repr”完全符合我的要求。我将在允许的情况下尽快标记为已回答。我需要如何设置格式,以便列表的每个索引显示在不同的行上?或
str
。对于列表,他们会做同样的事情。这是真的。不过,OP应该注意非列表对象。例如,如果他想在文件中写入字符串、引号和所有内容,
repr
str
更可取。是的,对于其他类型,这完全取决于你想要什么
print
在传递任意对象时使用
str
,而交互式解释器的REPL使用
repr
@Kevin非常感谢您的帮助!使用“repr”完全符合我的要求。我将在允许的情况下尽快标记为已回答。我需要如何设置格式,以便列表的每个索引显示在不同的行上?或
str
。对于列表,他们会做同样的事情。这是真的。不过,OP应该注意非列表对象。例如,如果他想在文件中写入一个字符串,则使用引号和全部,
r