繁体   English   中英

是否可以通过Python中的.txt文件在另一个列表中列出列表

[英]Is it possible list inside another list through a .txt file in Python

我有一些问题,如何读取.txt文件,并通过此文件在其他列表中构建一个列表。

看:

我有这个文件:

1 2 3
4 5 6
7 8 9

现在我正在使用numpy来读取文件:

DataIn = numpy.loadtxt('jpegOut.txt')

回报是这样的:

print DataIn
[[1 2 3]
[4 5 6]
[7 8 9]]

但我需要这个:

[[1 2 3],
 [4 5 6],
 [7 8 9]]

你可以帮帮我吗?

提前致谢

没有NumPy

with open('jpegOut.txt', 'r') as f:
    data_in = [[line] for line in f]

使用.tolist()将numpy数组转换为python列表:

numpy.loadtxt('jpegOut.txt').tolist()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM