[英]AttributeError: '_io.TextIOWrapper' object has no attribute 'reader' [Worse Problem]
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 .我知道还有其他人有这个问题,比如Seperate File Named csv.py和The name of the code is csv 问题。 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.我检查了 csv.文件,但除了库之外没有其他文件。 I also checked the name, as you can see;
如您所见,我还检查了名称; but I don't know the problem.
但我不知道问题所在。 Do you know any ways of fixing it?
你知道修复它的任何方法吗?
Your code gives no error.您的代码没有错误。
But it gives you an error if you call the main
function in last.但是如果你最后调用
main
function,它会给你一个错误。
This error is because you make a variable csv
这个错误是因为你做了一个变量
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.