Apr-28-2020, 02:00 PM
I am trying to create a simple python programme that takes some user input and plots the data. So I decided to use a class to make the programme versatile enough that you can keep calling the class and input new information and plot data. The programme currently asks for the name of a file and identifies if that file is a text or csv file. The programme has some built in tests to determine if the input was valid and asks for the user to input again if an error or incorrect file name is added.
The problem I have is that I want to create another function within the class that you can call that takes the data from the text/csv file and allows you to plot it using the plotting abilities within python. However, after the user has input and the data is extracted from the file I cannot return any of the information into the __init__ part of the class so that it can be used for other functions within the class. I am not sure how to go about correcting this issue or whether my method is too complicated as I am new to python programming and I am not fully competent when using classes in python. Any help will be much appreciated.
The problem I have is that I want to create another function within the class that you can call that takes the data from the text/csv file and allows you to plot it using the plotting abilities within python. However, after the user has input and the data is extracted from the file I cannot return any of the information into the __init__ part of the class so that it can be used for other functions within the class. I am not sure how to go about correcting this issue or whether my method is too complicated as I am new to python programming and I am not fully competent when using classes in python. Any help will be much appreciated.
import pandas as pd import numpy as np import matplotlib.pyplot as plt class file_load: def __init__(self, name, xdata, ydata): self.name = name self.xdata = xdata self.ydata = ydata @classmethod def user_input(self): while True: try: name = input('Enter file name') if '.''txt' in name: print('File name: ' + name) data = pd.read_csv(name, delimiter = '\t') print(data.head(10)) xdata = data.columns[0] ydata = data.columns[1] break elif '.''csv' in name: print('File name: ' + name) while True: try: data = pd.read_csv(name) xdata = data[data.columns[0]] ydata = data[data.columns[1]] plotting = input('Do you want to plot the data?').lower() if plotting.startswith('y'): print('PLOTTING') break elif plotting.startswith('n'): print('END') break else: continue except pd.errors.ParserError: while True: try: row = input('Enter data start row') data = pd.read_csv(name, header = int(row)) print(data.head(10)) xdata = data[data.columns[0]] ydata = data[data.columns[1]] break except pd.errors.ParserError: print('ERROR: INCORRECT ROW START') print('Enter data start row: ') continue break print('ERROR: FILE UNKNOWN') print('Please enter file name:') continue except FileNotFoundError: print('ERROR: FILE NOT FOUND') print('Enter file name: ') continue break file_load.user_input()