如何使用 Python 将文本文件转换为二维数组?

How to convert a text file to 2D array using Python?

我有一个文本文件:

ABCDEFGHIJKL.MN

我的期望输出: AB 光盘 英孚 生长激素 IJ 吉隆坡

I tried this :
f = open("test.txt", "r")
output_list = []
for rec in f:
    chars = list(rec.strip())
    output_list.append(chars)
    print(chars)

但是它 return : ['A', 'B', 'C', ....]

任何想法,请

假设您想从多行文本创建一个二维数组, 请尝试以下操作:

with open("test.txt", "r") as f:
    output_list = []
    for rec in f.read().splitlines():
        rec = rec[:-3]  # remove 3 last characters
        list = [rec[i:i+2] for i in range(0, len(rec), 2)]
        output_list.append(list)

print output_list

其中 "test.txt" 看起来像:

ABCDEFGHIJKL.MN
OPQRSTUVWX-YZ

和输出:

[['AB', 'CD', 'EF', 'GH', 'IJ', 'KL'], ['OP', 'QR', 'ST', 'UV', 'WX']]