Python Forum
How to pass multiple arguments into function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass multiple arguments into function
#1
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'
Reply
#2
You defined function with 3 parameter, you passed only 2

EDIT: self reserved for class instance
Reply
#3
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?
Reply
#4
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')
Reply
#5
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 848 Jul-27-2023, 12:40 AM
Last Post: tester_V
  calling external function with arguments Wimpy_Wellington 7 1,423 Jul-05-2023, 06:33 PM
Last Post: deanhystad
Sad Iterate randint() multiple times when calling a function Jake123 2 2,037 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 3,571 Sep-06-2021, 03:43 PM
Last Post: muzikman
  'namespace' shorthand for function arguments? shadowphile 5 2,576 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  Checking the number of arguments a function takes Chirumer 3 2,150 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Function - Return multiple values tester_V 10 4,434 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Possible to dynamically pass arguments to a function? grimm1111 2 2,160 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Why Pass Functions as arguments? muzikman 14 5,589 Jan-18-2021, 12:08 PM
Last Post: Serafim
  how to pass arguments between pythons scripts? electricDesire 2 2,142 Oct-19-2020, 07:19 PM
Last Post: electricDesire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020