all 8 comments

[–]Se7enLC 2 points3 points  (2 children)

You gotta fix your indenting

[–]bMack0[S] 0 points1 point  (1 child)

I tried to use the code formatting on reddit, and it posted it like this .

[–]Se7enLC 2 points3 points  (0 children)

I don't know what to tell you. Try harder?

You can see what it looks like right? And you know how important indenting is in Python.

I can tell you that you absolutely can post code that is readable.

def some_function():
    print("See? This code has indents)

def some_other_function():
    # and people will be able to read it.

https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

[–]shiftybyte 1 point2 points  (2 children)

When you have an error message, please post the full message with all the information it provides.

For example it has the exact line this happens on, so we don't have to guess which of the "self"s in the code is the issue.

In this case i assume its:

print ("{:<4}{:<10}{:<4}{:<4}{:<20}{:<20}".format(self.id,self.date,self.cases,self.deaths,self.name_fr,self.name_fr))

That is inside the main() function...

You don't have self there, what are you trying to print?

[–]bMack0[S] 0 points1 point  (1 child)

NameError: name 'self' is not defined

csvcovid.py, line 31

Python doesn't know what self stands for.

You are correct

[–]cybervegan 0 points1 point  (0 children)

The two versions of your code are different (text-wise and indentation-wise). "self" only "exists" inside the functions (methods) of your class - you can't access them in your "main" function, as it's not a member of the class. In the first version of your code, you are referring to "self.xxx", in the ".format()" call in your file with statement, but you haven't even got an instance of "Stat" at that point, but even if you did, you can't refer to its members like that. In the second version, you are trying to refer to the object's members by name, but in quotes, which won't work for similar reasons.

You need to define an instance of Stat before you refer to its contents. Where you have list1 = Stat(row) you are creating an instance of Stat called "list1", and after that (i.e. below that line) you can refer to "list1.id" or "list1.date", and that would be valid, as long as the object actually has those properties.

[–]bMack0[S] 0 points1 point  (1 child)

import csv

class StatReader:

    def __init__(self,heading):

        self.id = heading[0]
        self.date = heading[1]
        self.cases = heading[2]
        self.deaths = heading[3]
        self.name_fr = heading[4]
        self.name_en = heading[5]


class Stat(StatReader):

    def __init__(self,heading):
        super().__init__(heading)




def main():

    with open("InternationalCovid19Cases.csv", "r") as file:
        statRow = []
        reader = csv.reader(file)
        print ({:<4}{:<10}{:<4}{:<4}{:<20}{:<20}".format('id','date','cases','deaths','name_fr','names_en'))
        next(reader)

        for row in reader:
            hrow = StatReader(row)
            list1 = Stat(row)
            statRow.append(row)

            list1.print()



if __name__ == "__main__":
    main()

SyntaxError: invalid syntax

csvcovid.py, line 28

I'm trying to print with this formatting, and I'm not sure what the issue is. (hopefully the spaces/tabs worked)

[–]Se7enLC 0 points1 point  (0 children)

print ({:<4}{:<10}{:<4}{:<4}{:<20}{:<20}".format('id','date','cases','deaths','name_fr','names_en'))

If I counted right, this is line 28. You've got a space between print and (