Python Forum

Full Version: How to pass multiple arguments into function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I want to read a CSV file and I defined one function class, but my program gives error.
my input csv file to read:

col1  col2 	col3
A	  2	    45
C	  21	71
V	  63	9
M	  45	0
I use the below code:

logging.basicConfig(filename='temp_log.txt', level=logging.DEBUG)
logging.debug("Hello new log")
colName=['col1','col2']
logging.debug("Initialize file read...")
def read_data(self, inFile, colNames,mode):
    try:
        df_tmp= pd.read_csv(inFile)
        self.mode=mode
        self.colNames=colNames
        print(mode)
        print(colName)
        df=df_tmp.loc[df_tmp[colNames]]
        logging.debug("Read data successfully")
        print(df.head)
    except OSError as e:
        logging.debug("File read fail check fail reason below")
        logging.debug(e.errno)

data =read_data(r'D:\PythonCodes\inFile.csv','col1', 'NOM') 
error:
TypeError: read_data() missing 1 required positional argument: 'mode'
You defined function with 3 parameter, you passed only 2

EDIT: self reserved for class instance
I pased these areguments:
1. r'D:\PythonCodes\inFile.csv'
2. 'col1'
3. 'NOM'
I am new to the classes, where to edit kidly guide?
Remove self from your code or

import logging
import pandas as pd


class MyData:

    def __init__(self):

        self.mode = None
        self.colNames = []

    def read_data(self, inFile, colNames, mode):
        try:
            df_tmp = pd.read_csv(inFile, sep=';')
            self.mode = mode
            self.colNames = colNames
            print(mode)
            print(colName)
            df = df_tmp[colNames]
            logging.debug("Read data successfully")
            print(df.head)
        except OSError as e:
            logging.debug("File read fail check fail reason below")
            logging.debug(e.errno)


logging.basicConfig(filename='temp_log.txt', level=logging.DEBUG)
logging.debug("Hello new log")
logging.debug("Initialize file read...")

data = MyData()
data.read_data(r'inFile.csv', 'col1', 'NOM')
Thanks, it works, but I want to pass df to another function I use below code, but it gives
AttributeError: 'MyData' object has no attribute 'df'
How to pass the variables from one function another?

import logging
import pandas as pd
 
class MyData:
 
    def __init__(self):
 
        self.mode = None
        self.colNames = []
    def read_data(self, inFile, colNames, mode):
        try:
            df_tmp = pd.read_csv(inFile)
            self.mode = mode
            self.colNames = colNames
            print(mode)
            print(colNames)
            df = df_tmp[colNames]
            logging.debug("Read data successfully")
            print(df.head)
            data.mod_data()
        except OSError as e:
            logging.debug("File read fail check fail reason below")
            logging.debug(e.errno)
    def mod_data(self):
        self.df=(data.df)
        print("New Df.......")
        print(df)
 
logging.basicConfig(filename='temp_log.txt', level=logging.DEBUG)
logging.debug("Hello new log")
logging.debug("Initialize file read...")
 
data = MyData()
data.read_data(r'inFile.csv', 'col1', 'NOM')