从文件中写入和读取列表 [英] Write and read a list from file

查看:41
本文介绍了从文件中写入和读取列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有点奇怪的请求,但我正在寻找一种将列表写入文件然后在其他时间读回来的方法.

我无法重新制作列表以使其正确形成/格式化,如下例所示.

我的列表有如下数据:

测试数据这里这个是一个团体 :)测试数据这里这个是另一个团体 :)

解决方案

如果您不需要它是人类可读/可编辑的,最简单的解决方案是使用 pickle.

写:

 with open(the_filename, 'wb') as f:pickle.dump(my_list, f)

阅读:

 with open(the_filename, 'rb') as f:my_list = pickle.load(f)

<小时>

如果您确实需要它们是人类可读的,我们需要更多信息.

如果 my_list 保证是一个没有嵌入换行符的字符串列表,只需每行写一个:

 with open(the_filename, 'w') as f:对于 my_list 中的 s:f.write(s + '
')使用 open(the_filename, 'r') 作为 f:my_list = [line.rstrip('
') for line in f]

<小时>

如果它们是 Unicode 字符串而不是字节字符串,您需要对它们进行编码.(或者,更糟糕的是,如果它们是字节字符串,但不一定与您的系统默认编码相同.)

如果它们可能有换行符或不可打印的字符等,您可以使用转义或引用.Python 在 stdlib 中内置了多种不同类型的转义.

让我们在这里使用 unicode-escape 一次性解决以上两个问题:

 with open(the_filename, 'w') as f:对于 my_list 中的 s:f.write((s + u'
').encode('unicode-escape'))使用 open(the_filename, 'r') 作为 f:my_list = [line.decode('unicode-escape').rstrip(u'
') for line in f]

<小时>

您也可以在 2.x 中使用 3.x 风格的解决方案,其中任一编解码器 模块或 io 模块:*

导入io使用 io.open(the_filename, 'w', encoding='unicode-escape') 作为 f:f.writelines(line + u'
' for line in my_list)使用 open(the_filename, 'r') 作为 f:my_list = [line.rstrip(u'
') for line in f]

* TOOWTDI,那么哪个是明显的方法?这取决于…… 对于简短版本:如果您需要使用 2.6 之前的 Python 版本,请使用 codecs;如果没有,请使用 io.

This is a slightly weird request but I am looking for a way to write a list to file and then read it back some other time.

I have no way to remake the lists so that they are correctly formed/formatted as the example below shows.

My lists have data like the following:

test
data
here
this
is one
group :)

test
data
here
this
is another
group :)

解决方案

If you don't need it to be human-readable/editable, the easiest solution is to just use pickle.

To write:

with open(the_filename, 'wb') as f:
    pickle.dump(my_list, f)

To read:

with open(the_filename, 'rb') as f:
    my_list = pickle.load(f)


If you do need them to be human-readable, we need more information.

If my_list is guaranteed to be a list of strings with no embedded newlines, just write them one per line:

with open(the_filename, 'w') as f:
    for s in my_list:
        f.write(s + '
')

with open(the_filename, 'r') as f:
    my_list = [line.rstrip('
') for line in f]


If they're Unicode strings rather than byte strings, you'll want to encode them. (Or, worse, if they're byte strings, but not necessarily in the same encoding as your system default.)

If they might have newlines, or non-printable characters, etc., you can use escaping or quoting. Python has a variety of different kinds of escaping built into the stdlib.

Let's use unicode-escape here to solve both of the above problems at once:

with open(the_filename, 'w') as f:
    for s in my_list:
        f.write((s + u'
').encode('unicode-escape'))

with open(the_filename, 'r') as f:
    my_list = [line.decode('unicode-escape').rstrip(u'
') for line in f]


You can also use the 3.x-style solution in 2.x, with either the codecs module or the io module:*

import io

with io.open(the_filename, 'w', encoding='unicode-escape') as f:
    f.writelines(line + u'
' for line in my_list)

with open(the_filename, 'r') as f:
    my_list = [line.rstrip(u'
') for line in f]

* TOOWTDI, so which is the one obvious way? It depends… For the short version: if you need to work with Python versions before 2.6, use codecs; if not, use io.

这篇关于从文件中写入和读取列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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