-1

I am trying to insert data from a .txt file to a SQLite database but whenever I run my code I get this error: NameError: name 'get_value' is not defined. Is this because im trying to call a function in a function inside of a class? I didn't include the other parts of my code because I didn't think they are needed but if they are let me know and I'll add it.

Python File:

class InputFromIPLog:
    
    ...

    def get_value():
        info = item.strip().split(':')
        val = info[1].strip().split(',')
        return val[0].strip()

    def insert_data():
        with open(f"new_iplog.txt", "r") as f:
            file_data = f.readlines()

        input_gamertag = ""
        input_ip_address = ""
        input_xuid = ""
        input_mid = ""

        for item in file_data:
            if "Gamertag" in item:
                input_gamertag = get_value(item)
            elif "IP Address" in item:
                input_ip_address = get_value(item)
            elif 'XUID' in item:
                input_xuid = get_value(item)
            elif 'MID' in item:
                input_mid = get_value(item)

        cursor.execute("INSERT INTO userinfo (gamertag, ip, xuid, mid) values(?, ?, ?)", (input_gamertag, input_ip_address, input_xuid, input_mid,))

...

new_iplog.txt File:

...

Gamertag: UnknownUser                       
IP Address: 10.0.0.1
XUID: 000301F23B9F6ED4
MID: FB00FDA037CEB46E

Gamertag: AnotherUnknownUser                   
IP Address: 1.1.1.1
XUID: 0009000005E96E9B
MID: FB00F5413910FEDD

...
Brian
  • 67
  • 6
  • I can't find a dupe, but one surely exists so I won't post an answer. You need `self.get_value` though to refer to the method. Methods names aren't in a scope on their own. You need to explicitly call the method on an instance of the class (`self` in this case). – Carcigenicate Mar 29 '21 at 22:24
  • [This](https://stackoverflow.com/questions/14086830/python-calling-method-in-class) is a dupe that explains it, but it's a terrible dupe. – Carcigenicate Mar 29 '21 at 22:25

1 Answers1

1

I don't know if you have other attributes tied to the instance of this class, but generally to access methods and attributes of a class you should be using self.

Here's a Intro to Designing Classes page which goes over self and how it is used.

With my limited view of your problem, something like below might work:

class InputFromIPLog:
    ...

    def get_value(self, item):
        info = item.strip().split(':')
        val = info[1].strip().split(',')
        return val[0].strip()

    def insert_data(self):
        with open(f"new_iplog.txt", "r") as f:
            file_data = f.readlines()

        input_gamertag = ""
        input_ip_address = ""
        input_xuid = ""
        input_mid = ""

        for item in file_data:
            if "Gamertag" in item:
                input_gamertag = self.get_value(item)
            elif "IP Address" in item:
                input_ip_address = self.get_value(item)
            elif 'XUID' in item:
                input_xuid = self.get_value(item)
            elif 'MID' in item:
                input_mid = self.get_value(item)

        cursor.execute("INSERT INTO userinfo (gamertag, ip, xuid, mid) values(?, ?, ?)",
                       (input_gamertag, input_ip_address, input_xuid, input_mid,))


...

Also, in your code, get_value takes no parameters, but when you use it you pass item to it. I've updated the arguments of the function in the code above to reflect your usage.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57