簡體   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