繁体   English   中英

格式化二维列表,从文件中读取

[英]Formatting a 2d list, read in from file

使用python 2.7,我可以从文件名列表中读取多个csv ...

empty = []
for i in items:
    with open(i) as file:
        reader = csv.reader(file, dialect='excel')
        for row in reader:
            print row

这可以很好地打印...

['myfile.csv', 'a', '4', '50.0', 10.0]
['myfile2.csv', 'b', '2', '20.0', 50.0]
......

现在,如果我想将此打印后的列表附加到一个空的第二个列表中(空= []),如何格式化它,使其看起来像上面的输出(整洁的列)。 当我添加empty.append(row)时,这是一个很大的,连续的连续输出empty.append(row)

尝试使用pprint:

>>> from pprint import pprint as pp
>>> a = [1,2,3,4,4,5,5,6,6,7]
>>> pp(a)
[1, 2, 3, 4, 4, 5, 5, 6, 6, 7]

>>> b = [a, a, a]
>>> pp(b)
[[1, 2, 3, 4, 4, 5, 5, 6, 6, 7],
 [1, 2, 3, 4, 4, 5, 5, 6, 6, 7],
 [1, 2, 3, 4, 4, 5, 5, 6, 6, 7]]
>>> 

暂无
暂无

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

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