How to pass multiple arguments into function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to pass multiple arguments into function (/thread-28207.html) |
How to pass multiple arguments into function - Mekala - Jul-09-2020 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 0I 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' RE: How to pass multiple arguments into function - mrdominikku - Jul-09-2020 You defined function with 3 parameter, you passed only 2 EDIT: self reserved for class instance RE: How to pass multiple arguments into function - Mekala - Jul-09-2020 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? RE: How to pass multiple arguments into function - mrdominikku - Jul-09-2020 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') RE: How to pass multiple arguments into function - Mekala - Jul-11-2020 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') |