打印不同方式的CSV

2024-10-04 09:20:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

team1, 100
team2, 200
team3, 300

我发现我对Python/英语的知识还不足以理解这里其他主题的解释,所以我不得不要求你尽可能简单 这是我正在使用的一段代码

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)

Tags: 文件csv代码主题列表readdefopen
2条回答

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

for team in a:
    print("{}, {}".format(team[0], team[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])

相关问题 更多 >