Reputation: 21
I was writing my code for a calendar with events, but it keeps giving me this error,
Traceback (most recent call last):
File "/Users/Local Drive/Documents/Other/calendar_if_it_works.py", line 42, in <module>
reader = csv.reader(csvfile)
AttributeError: '_io.TextIOWrapper' object has no attribute 'reader'
I know that there are other people with the problem, like Seperate File Named csv.py and The name of the code is csv problem. But when I try those, they don't work.
And here is my code:
from tkinter import *
from tkcalendar import Calendar
import csv
import itertools as itt
from operator import itemgetter
def unique_rows(rows):
for key, group in itt.groupby(rows, key=itemgetter(0)):
yield (key,) + tuple(itt.chain.from_iterable(r[1:] for r in group))
def main():
with open('csv_file.csv') as ifh, open('output.csv', 'w') as ofh:
rd = csv.reader(ifh)
wt = csv.writer(ofh)
wt.writerows(unique_rows(rd))
if __name__ == '__main__':
main()
records = {}
mtoflag = False
line = 0
ncol=0
with open('output.csv', 'r') as csv:
first_line = csv.readline()
your_data = csv.readlines()
for i in range(len(your_data)):
ncol = your_data[i].count(',') + 1
print(your_data[i])
print(ncol)
if ncol > 2:
mtoflag = True
line = your_data[i]
break
print(mtoflag)
print(line)
print("Column:", ncol)
with open('output.csv', mode='r') as csvfile:
reader = csv.reader(csvfile, deliminter=",")
for rows in reader:
if mtoflag == True:
if rows == line:
records[rows[0]] = list(rows[1], rows[columns-1])
print(records)
else:
records[rows[0]] = rows[1]
print(records)
# creating an object of tkinter
tkobj = Tk()
# setting up the geomentry
tkobj.title("Calendar picker")
#creating a calender object
tkc = Calendar(tkobj,selectmode = "day",year=2022,month=4,date=1)
#display on main window
tkc.pack(pady=40)
# getting date from the calendar
def fetch_date():
edate = tkc.get_date()
if edate == "":
edate="None Selected"
date.config(text = "Selected Date is: " + edate)
recval = []
recval.append(records.get(edate))
print("Record Values:", recval)
events.delete(0, END)
events.insert(END, recval)
#add button to load the date clicked on calendar
but = Button(tkobj,text="Select Date",command=fetch_date)
#displaying button on the main display
but.pack()
#Label for showing date on main display
date = Label(tkobj,text="",bg='black',fg='white')
date.pack(pady=20)
# Event Label indicator
eventind = Label(tkobj, text="Events:", bg='black', fg='white')
eventind.pack()
# Event Listbox for showing events
events = Listbox(tkobj, width=34, height=20)
events.pack()
#starting the object
tkobj.mainloop()
I checked csv.file but there was no other file besides the library. I also checked the name, as you can see; but I don't know the problem. Do you know any ways of fixing it?
Upvotes: 2
Views: 224
Reputation: 6224
Your code gives no error.
But it gives you an error if you call the main
function in last.
This error is because you make a variable csv
Try this code.
from tkinter import *
from tkcalendar import Calendar
import csv
import itertools as itt
from operator import itemgetter
def unique_rows(rows):
for key, group in itt.groupby(rows, key=itemgetter(0)):
yield (key,) + tuple(itt.chain.from_iterable(r[1:] for r in group))
def main():
with open('csv_file.csv') as ifh, open('output.csv', 'w') as ofh:
rd = csv.reader(ifh)
wt = csv.writer(ofh)
wt.writerows(unique_rows(rd))
records = {}
mtoflag = False
line = 0
ncol=0
with open('output.csv', 'r') as csv_: # changing the variable name
first_line = csv_.readline()
your_data = csv_.readlines()
for i in range(len(your_data)):
ncol = your_data[i].count(',') + 1
print(your_data[i])
print(ncol)
if ncol > 2:
mtoflag = True
line = your_data[i]
break
if __name__ == '__main__':
main()
Upvotes: 1