Python 2.7 -从文件中编写和读取列表

时间:2022-11-09 20:23:24

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 :)

2 个解决方案

#1


93  

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

如果您不需要它是人类可读/可编辑的,最简单的解决方法就是使用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:

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

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

with open(the_filename, 'r') as f:
    my_list = [line.rstrip('\n') 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.)

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

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.

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

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

让我们在这里使用unicodeescape来同时解决上述两个问题:

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

with open(the_filename, 'r') as f:
    my_list = [line.decode('unicode-escape').rstrip(u'\n') 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:*

你也可以用3。x样式解决方案2。使用codecs模块或io模块:*

import io

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

with open(the_filename, 'r') as f:
    my_list = [line.rstrip(u'\n') 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.

* TOOWTDI,哪一种是最明显的?这取决于……对于简短的版本:如果您需要在2.6之前使用Python版本,请使用codecs;如果没有,使用io。

#2


12  

As long as your file has consistent formatting (i.e. line-breaks), this is easy with just basic file IO and string operations:

只要你的文件有一致的格式(即换行),这很容易与基本的文件IO和字符串操作:

with open('my_file.txt', 'rU') as in_file:
    data = in_file.read().split('\n')

That will store your data file as a list of items, one per line. To then put it into a file, you would do the opposite:

将数据文件存储为项目列表,每行一个。然后把它放进一个文件,你会做相反的事情:

with open('new_file.txt', 'w') as out_file:
    out_file.write('\n'.join(data)) # This will create a string with all of the items in data separated by new-line characters

Hopefully that fits what you're looking for.

希望它符合你的要求。

#1


93  

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

如果您不需要它是人类可读/可编辑的,最简单的解决方法就是使用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:

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

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

with open(the_filename, 'r') as f:
    my_list = [line.rstrip('\n') 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.)

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

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.

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

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

让我们在这里使用unicodeescape来同时解决上述两个问题:

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

with open(the_filename, 'r') as f:
    my_list = [line.decode('unicode-escape').rstrip(u'\n') 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:*

你也可以用3。x样式解决方案2。使用codecs模块或io模块:*

import io

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

with open(the_filename, 'r') as f:
    my_list = [line.rstrip(u'\n') 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.

* TOOWTDI,哪一种是最明显的?这取决于……对于简短的版本:如果您需要在2.6之前使用Python版本,请使用codecs;如果没有,使用io。

#2


12  

As long as your file has consistent formatting (i.e. line-breaks), this is easy with just basic file IO and string operations:

只要你的文件有一致的格式(即换行),这很容易与基本的文件IO和字符串操作:

with open('my_file.txt', 'rU') as in_file:
    data = in_file.read().split('\n')

That will store your data file as a list of items, one per line. To then put it into a file, you would do the opposite:

将数据文件存储为项目列表,每行一个。然后把它放进一个文件,你会做相反的事情:

with open('new_file.txt', 'w') as out_file:
    out_file.write('\n'.join(data)) # This will create a string with all of the items in data separated by new-line characters

Hopefully that fits what you're looking for.

希望它符合你的要求。