I'm creating a work log in python where a user can enter a task or can lookup a task by date. My initial prompt asks user to either enter a task or lookup by date. If the user starts by looking up by date--the program works correctly and displays all dates. If a user starts by adding a task, and then looking up tasks by date, the program displays an 'object does not support indexing error'. I think for some reason, the list is getting emptied, but i can't for the life of me understand where or when this might be happening. Here is main worklog file:
import csv
import re
import datetime
from task import Task
from task_list import TaskList
class Worklog():
def __init__(self):
self.filename = "work_log.csv"
self.tasklist = TaskList()
self.tasklist.read_task_from_file(self.filename)
def search_by_date(self):
for d, i in enumerate(self.tasklist.dates()):
print(d+1, ':', i)
# while True:
# datereq = input("Select Number To See Tasks For A Date").strip().lower()
# try:
# datereq = int(datereq)
# return datereq
# except ValueError:
# print("Invalid Entry. Please try again")
# continue
def search_by_time(self):
pass
def exact_search(self):
pass
def pattern_search(self):
pass
def add_task(self):
task = Task()
task.input_task()
task.input_minutes()
task.input_notes()
task.date = datetime.date.today()
self.tasklist.app_task(task)
self.tasklist.save_task_to_file(self.filename,task)
def lookup_task(self):
if len(self.tasklist.task_list) == 0:
print("Your task log is empty.\n")
input("Hit Enter to add a new task.")
else:
while True:
lookup = input("Lookup by Date(D), Time(T), Exact Search(E) or Pattern(P): ")
lookup.lower()
if lookup == 'd':
self.search_by_date()
break
elif lookup == 't':
self.search_by_time()
break
elif lookup == 'e':
self.exact_search()
break
elif lookup == 'p':
self.pattern_search()
break
else:
print("Sorry invalid option. Please try again")
def start_message(self):
while True:
q = input("Add New Task(1) or Lookup Task(2) or Quit(3): ".strip().lower())
if q == "1":
self.add_task()
elif q == "2":
self.lookup_task()
elif q == "3":
exit()
else:
print("Sorry that's an invalid entry. Please try again.")
continue
if __name__ == '__main__':
log = Worklog()
log.start_message()
The error is pointing to the dates function which is shown below along with a few of the other methods for my 'task-list' class. Is there an issue with the way that I am indexing this list? Or am i right in that the task_list list is getting reset somehow when the user enters the second step of the loop. Thanks:
def app_task(self, task):
self.task_list.append(task)
def save_task_to_file(self,filename,task):
with open(filename, 'a', newline="\n", encoding="utf-8") as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile, delimiter=",")
# writing the data rows
csvwriter.writerow([task.date, task.task, task.minutes, task.notes])
def read_task_from_file(self,filename):
with open(filename, 'r') as csvfile:
task_reader = csv.reader(csvfile, delimiter=',')
for row in task_reader:
task = Task()
self.task_list.append(row)
return self.task_list
def dates(self):
self.read_task_from_file("work_log.csv")
dates = []
for row in self.task_list:
date = row[0]
if row[0] not in dates:
dates.append(date)
return sorted(dates)
Note--here is an example of what the work_log.csv file looks like:
2017-03-23,gardening,30,not really
2017-03-23,bowling,30,none
2017-03-23,bowling,90,none
2017-03-23,bowling,93,none
2017-03-23,baseball,2,none
2017-03-23,bowling,20,none
2017-03-23,base,0,jj
2017-03-23,bowling,33,none
Added per Jake's recommendation:
def get_date(self):
for row in self.task_list:
d = row[0]
return d
def dates(self):
dates = []
for row in dates:
date = row.get_date()
if date not in dates:
dates.append(date)
return sorted(dates)
The issue appears to be where you call date = row[0]; this is because in the loop row will be a Task object as you are iterating over self.task_list. In this case you are trying to index into a Task object, which is not set up for indexing.
The simple solution for this would be to replace row[0] with row.date; which will directly access the date field of the row object, without needing to bother about indexing at all.
Related
Ok so I am trying really hard to get this working. I think I am still not so familiar with the self argument yet, not sure if that matters in this case but I want to append user input once I choose number 1 in the menu and input data that appends to user_list and eventually saves in csv file. But I only get the code <main.User object at 0x7f36d7ccdfa0> in the csv file once I put in the data throught the program
import csv
user_list = []
class User:
def __init__(self, first, last):
self.first = first
self.last = last
self.email = first + '.' + last + '#expressdelivery.com'
def menu():
while True:
print("[1]. Add/remove/update: user to the system.")
try:
option = int(input("Choose from the menu: "))
if option == 1:
user_list.append(User(first = input("Name: "), last = input("Last: ")))
with open('User.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow(user_list)
return(menu())
else:
print("Program quits")
break
except:
print("Error")
menu()
Basically im doing a project in python as part of my full stack course, and i have ran into wall.
My project is a parking lot system, which gets you a ticket with your cars number, unique code, time entered. At the exit you will have to write the unique code and it will calculate the cost for the parked time. I created a class named Key which creates the object it self with the exit time as None, but my trouble begins by trying to update the self.exit which doesn't work out for me using the the function exit_time. Every time a car enters the data is written to a file using pickle, i tried using a list to be able to edit the object but something isn't working out for me.
Also I tried many varieties by calling the function or what happens in the function it self.
THIS IS MY MAIN
list_of_cars = []
while True:
startmessage() # prints message
choice() # prints message
action = input("\nYour choice: ")
if action == "1":
choice_one() # prints message
car_number = input(" here: ")
new_client = Key(car_number)
list_of_cars.append(new_client)
writing_file(list_of_cars)
reading_file()
if action == "2":
choice_two() # prints message
exit_key = input(" here: ")
Key(exit_key).exit_time(exit_key)
READ AND WRITE
def reading_file():
with open("parking.data", "rb") as readfile:
list_of_cars = pickle.load(readfile)
print("testing", [str(x) for x in list_of_cars])
readfile.close()
return list_of_cars
def writing_file(list_of_cars):
with open("parking.data", "wb") as myfile:
pickle.dump(list_of_cars, myfile)
myfile.close()
return list_of_cars
THE CLASS
import random
import pickle
import datetime as dt
class Key:
def __init__(self, car_number):
self.car_number = car_number
self.key = self.generate()
self.enter = self.enter_time()
self.exit = None
"""
creates the key it self and while creating it checks if it already exists so that there will be no duplicates
"""
def generate(self):
key = ""
chunk = ""
alphabet = "ABCDEFGHIJKLMNOPQRZTUVWXYZ1234567890"
while True:
while len(key) < 8:
char = random.choice(alphabet)
key += char
chunk += char
if len(chunk) == 3:
key += "-"
chunk = ""
key = key[:-1]
with open("parking.data", "rb") as readfile:
new_list = pickle.load(readfile)
if key in new_list:
key = ""
else:
return key
def __str__(self):
return f"{self.car_number},{self.key}," \
f"{self.enter},{self.exit}"
def enter_time(self):
start = str(dt.datetime.now().strftime('%H:%M:%S'))
return start
def exit_time(self, exit_key):
from read_and_write import reading_file
list_of_cars = reading_file()
if exit_key in list_of_cars:
self.exit = str(dt.datetime.now().strftime('%H:%M:%S'))
print("IT WORKED!!!")
print("got to the function :/")
your conditional is:
if exit_key in list_of_cars:
exit_key is OM7-XVX
list_of_cars is ['5412,OM7-XVX,21:09:42,None1',...]
So...
'OM7-XVX' in ['5412,OM7-XVX,21:09:42,None1'] -> False
'OM7-XVX' in ['5412,OM7-XVX,21:09:42,None1'][0] -> True
Therefore replace the conditional with
if any(exit_key in v for v in list_of_cars):
I don't expect any coding answers, more just guidance. For my project I have to date-mine apple stock prices from a csv-file and implement it in my code. I provided a sample output below.
https://imgur.com/rPOPN1I
Right now, I am not getting any error messages but my code is not posting the columns requested from the csv.file. Are my definitions at fault or am I missing something else?
# Project No.: 5
# Author: burntchickennuget
# Description: making definitions, reading from a .csv file, validating input, data-mining,
f_list = list()
file_object = ()
my_tot = dict()
new_list = list()
def get_input_descriptor():
while True:
filename = input("Enter a file name: ")
if filename == 'table.csv':
with open(filename, "r") as infile:
infile.readlines()[1:]
# for line in lines:
# print(line.rstrip())
break
else:
print("Bad file name, try again")
return filename
def get_data_list(file_object, column_number):
dict = {}
for val in file_object:
date = val.split(",")[0]
data = float(val.split(",")[column_number])
dict[date] = data
return dict.items()
def average_data(new_list):
for date, price in new_list:
my_tot[date] = my_tot.get(date, 0) + float(price)
my_times[date] = my_times.get(date, 0) + 1
for key in my_tot:
f_list.append((float(my_tot[key] / my_times[key]), key))
def main():
get_input_descriptor()
column_number = int(input("Which column: "))
date_list = get_data_list(file_object, column_number)
final_list = average_data(date_list)
x = sorted(f_list)
print('Lowest 6:')
for tup in x[:6]:
print
tup[0], tup[1]
print('Highest 6:')
x = sorted(f_list, reverse=True)
for tup in x[:6]:
print
tup[0], tup[1]
while 1:
flag = input("do you want to continue? ")
if flag == '' or not flag[0].lower() in ['y', 'n']:
print("Please answer with a yes or no")
else:
break
if flag[0].lower() == 'y':
column = input("Which column: ")
print(column)
if flag[0].lower() == 'n':
print("Bye!")
if __name__ == "__main__":
main()
What can I try next?
Take a look around your get_input_descriptor method.
What are you returning from the method?
Where is the returned information being stored in the main method (is it being stored at all?)
What are you doing with the lines that you read from the file?
What is the file_object you are passing into get_data_list
My main advice would be to add print everywhere for debugging. See what it stored in your variables at different points in the program and see where a variable doesn't contain what you think it should
I have a text file set out in this layout:
Greg,Computer Science,Hard,5
Alex,Computer Science,Medium,2
Fiona,Maths,Easy,0
Cassie,Maths,Medium,5
Alex,Maths,Medium,1
In my program I want the user to be able to choose a certain name and see their results. My code for this looks like this:
name = input("Enter name: ")
for each in file:
each = each.split(",")
realName = each[0]
subject = each[1]
difficulty = each[2]
score = each[3]
if name == realName:
print(subject, difficulty, score)
break
else:
print()
print("Invalid name.")
name = input("Re-enter your name: ")
A few things are wrong with it though and I can't figure out what to do:
If the user entered "Alex", only one of his results will be displayed.
If a wrong name is inputted once, every other name inputted will return as "Invalid".
If the correct name is inputted and the results are displayed, the program will continue to ask for a name.
Does anybody have any solutions to these problems?
If you're going to query your file repeatedly, I'd recommend pre-loading your data once into a dictionary, and printing data as and when needed. Something like this:
data = {}
with open('file.txt', 'r') as file:
for line in file:
realName, subject, difficulty, score = each.split(',')
data.setdefault(realName, []).append((subject, difficulty, score))
while True:
name = input('>>> ')
data.get(name, 'Invalid Name')
This solves problems one and two. If you just want to break after the first valid name is input, you can query the return value of dict.get:
while True:
name = input('>>> ')
result = data.get(name)
if result:
print(result)
break
print('Invalid name')
This solves problem three.
You're better off using the csv module since your file syntax is simple CSV.
Then you can loop through the rows (each row will be an array of values).
import csv
def parse_csv_file(csv_file, operation, value, index):
with open(csv_file, newline='') as file:
reader = csv.reader(file, delimiter=',',
quotechar='|')
return operation(reader,
value, index)
def find_first_row(csv_reader, value, index):
for row in csv_reader:
if row[index] == value:
return row
return None
def main():
query = input('Enter a name: ')
result = parse_csv_file('file.csv',
find_first_row,
query, 0)
if result:
print(result)
else:
print('Nothing found!')
main()
I am new to python, and trying to create a program which opens a csv file. The user is supposed to enter a barcode , then the program finds that product and the cost of the product. However I got an error which is the title of my thread. Here is my code.
import csv # imports should go at the top of the file
def read_csv_file():
""" reads csv data and appends each row to list """
csv_data = []
with open("task2.csv") as csvfile:
spamreader = csv.reader(csvfile, delimiter=",", quotechar="|")
for row in spamreader:
csv_data.append(row)
return csv_data
def get_user_input():
""" get input from user """
while True:
try:
GTIN = int(input("input your gtin-8 number: "))
break
except:
print ("Oops! That was not a valid number. Try again")
def search_user_input():
""" search csv data for string """
search_user_input
gtin = get_user_input()
for row in PRODUCT_DATA:
#print(row) #debug print
if row[0] == str(gtin):
product = row[1]
price = round(float(row[2]),2)
print(product, price)
return(product, price)
repeat = input("not in there? search again? If so (y), else press enter to continue")
if repeat == 'y':
search_user_input() # calls function again in order to create a loop
def quantity():
gtin = 0
product = 0
price = 0.0
product_data = read_csv_file()
product,price = search_user_input()
product, price = search_user_input(str(gtin), product_price)
order = int(input("How much of " + product + " do you want?"))
price = round(price * order, 2)
print(quantity,price)
def order_making():
print("Apples")
PRODUCT_DATA = read_csv_file() # call function to read csv
quantity() # call the main function
I have cleaned the flow up a bit for search_user_input and quantity. In some cases you were calling search_user_input with multiple arguments (it doesn't accept them) and you made it recursive. I have added some comments in the code below. In fact, that function was returning None in your setup, which leads to the TypeError: 'NoneType' object is not iterable error.
The if __name__ == '__main__:' is not necessary in your code, I've included it more as a heads-up for later on when you'll want to start importing your own modules. See this for more info on this topic.
import csv
def read_csv_file():
""" reads csv data and appends each row to list """
csv_data = []
with open("task2.csv") as csvfile:
spamreader = csv.reader(csvfile, delimiter=",", quotechar="|")
for row in spamreader:
csv_data.append(row)
return csv_data
def get_user_input():
""" get input from user """
while True:
try:
GTIN = int(input("input your gtin-8 number: "))
return GTIN # Breaks the loop and returns the value
except:
print ("Oops! That was not a valid number. Try again")
def search_user_input(product_data): # Pass the csv data as an argument
""" search csv data for string """
keep_searching = True
while keep_searching:
gtin = get_user_input()
for row in product_data:
if row[0] == str(gtin):
product = row[1]
price = round(float(row[2]),2)
return(product, price)
repeat = input("not in there? search again? If so (y), else press enter to continue")
if repeat != 'y':
keep_searching = False
return None # This is done implicitly, I'm just making it obvious
def quantity():
product_data = read_csv_file()
matches = search_user_input(product_data)
if matches: # Will not be True if search_user_input returned None
product, price = matches[0], matches[1]
order = int(input("How much of {} do you want?".format(product)))
price = round(price * order, 2)
print("That costs {}".format(price))
if __name__ == '__main__': # You'll need this in future for importing modules
# There was no need to read the csv_data here and make it global
quantity() # call the main function