如何将嵌套列表写回文本文件以便每行有一个列表?

How to write nested list back to text file so that there is one list per line?

问题:

如何将嵌套列表从变量写入文本文件,以便嵌套列表中的每个列表都写入自己的行?

需要将该列表编译回文本文件,其格式与使用 f.readfiles() 函数将其拉出之前的格式完全相同。

除基于变量的解决方案和多变量解决方案外,我们也接受这些解决方案。 csv module 也是一个选项,但我宁愿在没有它的情况下先了解它的逻辑。

问题介绍

假设有一个 text file 在每一行中包含 list,如下所示:

['some_text','foo', some_int]
['some_text','foo', some_int]
['some_text','foo', some_int]
...

每个列表包含多个项目。所有行都希望从文本文件 read 到一个大的嵌套列表中并存储到 variable 中,如下所示:

variable = [['some_text','foo', some_int],
            ['some_text','foo', some_int],
            ['some_text','foo', some_int]]

上述变量中​​存储的信息格式无关紧要。存储在变量中的列表现在用一些 list tools 进行编辑。编辑完成后,包含编辑列表的变量再次写回同一个文本文件,用新的更改数据覆盖所有数据,如下所示:

['some_text','bar', some_int]
['edited_tx','foo', some_int]
['some_text','foo', edtd_int]
...

读台词

我设法用这段代码阅读了这些行:

with open("my_lists.txt") as f:
    listed_text = f.readlines()

文本文件中的所有文本现在都存储在一个名为 'listed_text' 的变量中,届时正在使用一些 list tools 首选进行编辑。

关于上面代码的更多信息在这里:

How do I read a file line-by-line into a list?

读写文件的代码示例不完整

这是本应对列表执行的操作的不完整且无效的编码示例。

# Let us open 'my_lists.txt' and read lines
# into a variable as a list.


def open_file():
    with open("my_lists.txt") as f:
        listed_text = f.readlines()
    list_item_editor(listed_text)

# Now let us replace 'foo' every index(1) in
# nested lists with word 'bar' with
# this imaginary editor 'list_item_editor()'.


def list_item_editor(argument):
    edited_list = []
    for item in nested_list:
        nested_list[item][i] = "bar"
    ...
    ...
    write_file(edited_list)

# Now we write the edited list back to 'my_lists.txt'
# by replacing the whole contents with new edited list.


def write_file(arg):
    with open("my_lists.txt", "w") as write_file:
        write_file.write(arg)

首选输出

这是首选输出的示例。嵌套列表中的每个项目(列表)都被写入它自己的行中。 'new line mark' \n 在文本文件中不可见。

# This is text_file.txt containing edited lists
# New line inserted after every list

['some_text','bar', some_int]
['edited_tx','foo', some_int]
['some_text','foo', edtd_int]
['some_text','bar', some_int]
['edited_tx','foo', some_int]
['some_text','foo', edtd_int]
['some_text','bar', some_int]
['edited_tx','foo', some_int]
['some_text','foo', edtd_int]

输入文件为:

['some_text','foo', 1]
['some_text','foo', 2]
['some_text','foo', 3]

这将 read/edit/write 文件:

import ast

with open('input.txt') as f:
    data = [ast.literal_eval(line) for line in f]

print(data)

data[0][1] = 'bar'
data[-1][-1] = 5

print(data)

with open('output.txt','w') as f:
    for line in data:
        f.write(str(line) + '\n')

但是将 Python 列表作为文本写入文件并不是最好的方法。更好的方法是使用 csv 模块,您愿意将输入文件更改为:

some_text,foo,1
some_text,foo,2
some_text,foo,3

并使用此代码:

import csv

with open('input.txt',newline='') as f:
    r = csv.reader(f)
    data = list(r)

print(data)

# Note, with csv, all columns are read as strings,
# so to do calculations convert:
data = [[a,b,int(c)] for a,b,c in data]

print(data)

data[0][1] = 'bar'
data[-1][-1] += 2

print(data)

with open('output.txt','w',newline='') as f:
    w = csv.writer(f)
    w.writerows(data)

此外,查看 pandas 第三方模块。