Manolo Viso Romero
Manolo Viso Romero

Reputation: 161

printing CSV in a different way

I need to print out a rankinglist from a csv file. I can get it printed out, that's not the problem but it will look like this:

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

I want it to print out like this:

team1, 100
team2, 200
team3, 300

I found that my knowledge of Python/ english language isn't high enough to understand what is being explained on other stackoverflow topics here so I have to ask you to be as simple as possible this is the piece of code that I'm using

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)

Upvotes: 1

Views: 78

Answers (2)

Hiskel Kelemework
Hiskel Kelemework

Reputation: 44

if you have exactly two elements in each list, this should work.

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

Upvotes: 1

Johan Abdullah Holm
Johan Abdullah Holm

Reputation: 41

It's not very elegant, but you can easily just iterate over the list:

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

Just add it after you made the list. Although a better way would be to either print it as you read it or use a dictionary.

Upvotes: 0

Related Questions