以不同的方式打印CSV

时间:2016-11-13 14:49:18

标签: python csv

我需要从csv文件中打印出排名列表。我可以打印出来,这不是问题,但它看起来像这样:

[['team1', '100'], ['team2', '200'], ['team3', '300']]

我希望它打印出来像这样:

team1, 100
team2, 200
team3, 300

我发现我对Python /英语的了解不够高,无法理解其他stackoverflow主题的解释,所以我必须要求你尽可能简单 这是我使用的代码片段

def read():
    a = open('casus.csv','r')
    Areader = csv.reader(a)
    a = []
    for row in Areader:
        if len (row) != 0:
            a = a + [row]
    print(a)

2 个答案:

答案 0 :(得分:1)

如果每个列表中只有两个元素,这应该可行。

def read():
    a = open('casus.csv','r')
    Areader = csv.reader(a)
    for row in Areader:
        if len(row != 0):
            print(row[0]+","+row[1])

答案 1 :(得分:0)

它不是很优雅,但您可以轻松地遍历列表:

for team in a:
    print("{}, {}".format(team[0], team[1]))

只需在列表后添加即可。虽然更好的方法是在阅读时打印它或使用字典。