如何使用python读取和写入表/矩阵到文件?

Yam*_*Mit 4 python file-io matrix tabular

我正在尝试创建一个程序,它接收数据并将其放在一个2×10的表中,只是在文本文件中.然后程序需要在以后的迭代中检索此信息.但我不知道该怎么做.我一直在研究numpty命令,常规文件命令以及尝试创建表的方法.但我似乎无法让这一切发挥作用.

以下是我要尝试制作的表的示例:

0    1    1    1    0    9    6    5
5    2    7    2    1    1    1    0
Run Code Online (Sandbox Code Playgroud)

然后我会检索这些值.有什么好办法呢?

Mat*_*ngo 8

为什么不使用该csv模块?

table = [[1,2,3],[4,5,6]]

import csv

# write it
with open('test_file.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    [writer.writerow(r) for r in table]

# read it
with open('test_file.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    table = [[int(e) for e in r] for r in reader]
Run Code Online (Sandbox Code Playgroud)

这种方法的另一个好处是可以制作其他程序可读的文件,比如Excel.

哎呀,如果你真的需要空间或制表符分隔,只需添加delimiter="\t"到你的读者和作家构造.