3

I want to read the CSV file that i have created, and have it printed on new lines:

this is the code

rows = []
with open("test.csv", 'r') as file:
    csvreader = csv.reader(file)
    header = next(csvreader)
    for row in csvreader:
        rows.append(row)
print(header)
print(rows)

the output I get is this...

['TeamName', 'MCount']
[[], ['team One', '23'], ['Team Two', '102'], ['Team Three', '44'], ['Team Four', '40']]

I want it to look like this:

Team One 23    
Team Two 102    
Team Three 44    
Team Four 40
Xiddoc
  • 3,369
  • 3
  • 11
  • 37
robbie2301
  • 47
  • 5
  • Does this answer your question? [How to print a list in Python "nicely"](https://stackoverflow.com/questions/1523660/how-to-print-a-list-in-python-nicely) – JNevill Mar 28 '22 at 14:27
  • What is the format of your source CSV? – Captain Caveman Mar 28 '22 at 14:28
  • @CaptainCaveman sorry i dont understand. – robbie2301 Mar 28 '22 at 14:39
  • Furthermore @JNevill i have used the link you sent and now have a list that is displayed on new lines, however there are still square brackets. how to remove these please? – robbie2301 Mar 28 '22 at 14:40
  • The test.csv file. What is the format of the csv? I see you have an empty element in your list. Also, are you wanting to write the data to another csv or just print it? – Captain Caveman Mar 28 '22 at 14:56
  • 1
    @CaptainCaveman only empty from testing, an accident. i am trying to make a working scoreboard for a college assignment – robbie2301 Mar 28 '22 at 15:01
  • You can use `for` loop to print out the data in any format you wish. – Code-Apprentice Mar 28 '22 at 15:06
  • Xiddoc has a good answer below that uses the code from the proposed duplicate to print out the inner list as it loops through the main list (you have a "list of lists") so you need that extra piece of `print(row[0], row[1])`. – JNevill Mar 28 '22 at 15:09
  • You can achieve the results you requested in two lines using Pandas. – Captain Caveman Mar 28 '22 at 15:11

3 Answers3

2

You can iterate over your rows and print each row in the format that you'd like:

# For each row
for row in rows:
    # Make sure the row is not empty
    if row:
        # Print the row
        print(row[0], row[1])

Or alternatively, you could use list comprehension to save it all as a string to a variable:

# For each row,
# if the row is not empty
# format it into a string.
# Join all the resulting strings together by a new line.
my_data_string = "\n".join([f"{row[0]} {row[1]}" for row in rows if row])
Xiddoc
  • 3,369
  • 3
  • 11
  • 37
1

This method will print in the format you requested and number your rows as well.

import pandas as pd

data = pd.read_csv('test.csv', header = None, names = ['Team Name', 'Number', 'Score'])

print(data)

Output:

      Team Name  Number  Score
0     Team One       23    NaN
1     Team Two       102   NaN
2     Team Three     44    NaN
3     Team Four      40    NaN
Captain Caveman
  • 1,448
  • 1
  • 11
  • 24
0

Now have one final problem;

this is the output:

'''

0 1 2

0 TeamName MCount Score

1 Team One 23 NaN

2 Team Two 234 NaN

3 Team Three 3 NaN

'''

the numbers on top i do not want

robbie2301
  • 47
  • 5
  • I see, the '012'. What is in the NaN cells? – Captain Caveman Mar 28 '22 at 15:38
  • 1
    i am now working on a way to input more data into this csv file, the scores. I am making a program that will ask the user 'which team won the event' then add 3 points to them. the NaN is because currently they are not assigned a score – robbie2301 Mar 28 '22 at 15:39
  • See my edit above. You can pass in the column header names, example: names = ['Team Name', 'Number']. – Captain Caveman Mar 28 '22 at 15:41
  • perfect! thankyou @CaptainCaveman . Would you be able to further assist with a way of assigning a score to each team? for example; i want to add 3 points to 'Team One' – robbie2301 Mar 28 '22 at 15:44
  • Happy to hear it. Please post another question with your updated code and specifically what you need help with for the new feature, rather than than expanding the scope of this post. If the answer resolves your issue, please upvote and accept the answer so others know it worked for you. Cheers. – Captain Caveman Mar 28 '22 at 15:46
  • Please edit the original question with this or make a new question! Answer section is only for answers – Emiliano Viotti Mar 28 '22 at 16:37