Home > Blockchain >  Print a blank row after each row of the result of a dictionary created with a class. I print only th
Print a blank row after each row of the result of a dictionary created with a class. I print only th

Time:01-16

This code, which works correctly, prints the results in a somewhat garbled way, because the content of each row is very long and because each row has no space with the following row. I would like to add space, i.e. an empty row, between each row with the key.

I tried so many ways to apply to print(test), for example with: * and sep="\n", with a for loop, with a range, with "\n", with join and other ways. The problem is that I always get the same result: I get the print with the space, but just print some keys without the dictionary values (which are taken from a class). Maybe i need to add the space inside the code and not directly in print

Currently printing goes like this (all in the same row):

{'Vegas-New York ': Info_Match(championship='NHL', date=8.1, round=9, clock='15:00', scorehome=[0, 1, 1, 5], scoreaway=[0, 0, 0, 3],'Minnesota-Dallas': Info_Match(championship='NHL', date=8.1, round=9, clock='18:00', scorehome=[1, 2, 3 , 4], scoreaway=[0, 2, 2, 2]

I would like this output:

{'Vegas-New York ': Info_Match(championship='NHL', date=8.1, round=9, clock='15:00', scorehome=[0, 1, 1, 5], scoreaway=[0, 0, 0, 3],

'Minnesota-Dallas': Info_Match(championship='NHL', date=8.1, round=9, clock='18:00', scorehome=[ 1, 2, 3, 4], scoreaway=[0, 2, 2, 2]

Or, this:

{'Vegas-New York':
  Info_Match(championship='NHL', date=8.1, round=9, clock='15:00', scorehome=[0, 1, 1, 5], scoreaway=[0, 0, 0, 3],

'Minnesota-Dallas':
Info_Match(championship='NHL', date=8.1, round=9, clock='18:00', scorehome=[1, 2, 3, 4], scoreaway=[0, 2, 2, 2]

Code

@dataclass
class Info_Match:
    championship: str
    date: float
    round: int
    clock: str
    scorehome: list[int]
    scoreaway: list[int]

test = {}

db = cursor_test.execute("sql code")

for row in db.fetchall():
    if row[0] not in test:
        info = Info_Match(
                championship=row[1],
                date=row[2],
                round=row[3],
                clock=row[4],
                scorehome=list(),
                scoreaway=list())


        test[row[0]] = info
    test[row[0]].scorehome.append(row[5])

print(test)

Thank you

CodePudding user response:

Once you have your dictionary (test),
you could just iterate through it using f-strings to produce the desired output:

for k,v in test.items():
    print(f"{k}: {v}", end='\n\n')

Code:

from dataclasses import dataclass

@dataclass
class Info_Match:
    championship: str
    date: float
    round: int
    clock: str
    scorehome: list[int]
    scoreaway: list[int]
    
test = {'Vegas-New York': Info_Match(championship='NHL', date=8.1, round=9, clock='15:00', scorehome=[0, 1, 1, 5], scoreaway=[0, 0, 0, 3]),'Minnesota-Dallas': Info_Match(championship='NHL', date=8.1, round=9, clock='18:00', scorehome=[1, 2, 3 , 4], scoreaway=[0, 2, 2, 2])}

for k,v in test.items():
    print(f"{k}: {v}", end='\n\n')

Output:

Vegas-New York: Info_Match(championship='NHL', date=8.1, round=9, clock='15:00', scorehome=[0, 1, 1, 5], scoreaway=[0, 0, 0, 3])

Minnesota-Dallas: Info_Match(championship='NHL', date=8.1, round=9, clock='18:00', scorehome=[1, 2, 3, 4], scoreaway=[0, 2, 2, 2])



Note:

To print your second option: you could use:

for k,v in test.items():
    print(f"{k}:\n{v}", end='\n\n')

Output:

Vegas-New York:
Info_Match(championship='NHL', date=8.1, round=9, clock='15:00', scorehome=[0, 1, 1, 5], scoreaway=[0, 0, 0, 3])

Minnesota-Dallas:
Info_Match(championship='NHL', date=8.1, round=9, clock='18:00', scorehome=[1, 2, 3, 4], scoreaway=[0, 2, 2, 2])
  • Related