Home > Blockchain >  function returns the correct value but stores none in text file
function returns the correct value but stores none in text file

Time:08-02

I get data from CSV store it in a class when I access a specific node value It prints the correct output which is 28 but in the file stores none. Do you know how i can store the value not none in my text file?

class node:
    Serial_No= None
    Keyword = None
    Country = None
    Difficulty = None
    Volume = None
    CPC = None
    CPS = None
    Parent_Keyword = None
    Last_Update = None
    SERP_Features = None
    Global_volume = None
    Traffic_potential = None
    def p(self):
        print(self.Difficulty)

def nodes():
    df = pd.read_csv('CleanedUpInput.csv')
    del(df['#'])
    with open('CleanedUpInput.csv','r') as f:
        reader = csv.DictReader(f)
        data=list(reader)
    row_list= []
    for index in range(0,len(df['Keyword'])):
        my_list=node()
        my_list.Serial_No = data[index]['Serial_No']
        my_list.Keyword = data[index]['Keyword']
        my_list.Country = data[index]['Country']
        my_list.Difficulty = data[index]['Difficulty']
        my_list.Volume = data[index]['Volume']
        my_list.CPC = data[index]['CPC']
        my_list.CPS = data[index]['CPS']
        my_list.Parent_Keyword = data[index]['Parent_Keyword']
        my_list.Last_Update = data[index]['Last_Update']
        my_list.SERP_Features = data[index]['SERP_Features']
        my_list.Global_volume = data[index]['Global_volume']
        my_list.Traffic_potential = data[index]['Traffic_potential']
        row_list.append(my_list)
        
    x = row_list[18].p()
    y = x.__str__()
    f = open("file2.txt","w")
    f.write(y)
    f.close()

CodePudding user response:

You need to return the value instead of printing it.

class node:
    Serial_No= None
    Keyword = None
    Country = None
    Difficulty = None
    Volume = None
    CPC = None
    CPS = None
    Parent_Keyword = None
    Last_Update = None
    SERP_Features = None
    Global_volume = None
    Traffic_potential = None
    def p(self):
        #print(self.Difficulty)
        return self.Difficulty

x = row_list[18].p()
with open("file2.txt","w") as f:
     f.write(x) #changed y to x
  • Related