[英]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.