TypeError: list indices must be integers

D

dubux

here is a function i wrote that doesnt work. i wrote to parse a "news"
file that is going to work in conjunction with a website via mod_wsgi.
my head hurts from looking at it so long. please help! i will further
explain in the post.

def news(x,y):
# usage news(date, number)
# x = date/news
# y = number
news_file = '/home/scam/Desktop/www/info/news'
news = open(news_file, 'r')
news_list = news.readlines()
news.close()
if x == 'date':
mylist = map(lambda i: news_list, filter(lambda i: i%2 == 0, range
(len(news_list))))
date = mylist[y]
return '<center>%s</center><br>' % (date)
if x == 'news':
mylist = map(lambda i: news_list, filter(lambda i: i%2 == 1, range
(len(news_list))))
news = mylist[y]
return '%s<br>' % (news)
else:
return news_list



and call it with the follow syntax: news('[date/news]', [any number])


i keep getting "TypeError: list indices must be integers" on the
following line "date = mylist[y]"
can someone please explain this and give me the proper way to achieve
what im trying to do?
 
M

Mensanator

here is a function i wrote that doesnt work. i wrote to parse a "news"
file that is going to work in conjunction with a website via mod_wsgi.
my head hurts from looking at it so long. please help! i will further
explain in the post.

def news(x,y):
� � � � # usage news(date, number)
� � � � # x = date/news
� � � � # y = number
� � � � news_file = '/home/scam/Desktop/www/info/news'
� � � � news = open(news_file, 'r')
� � � � news_list = news.readlines()
� � � � news.close()
� � � � if x == 'date':
� � � � � � � � mylist = map(lambda i: news_list, filter(lambda i: i%2 == 0, range
(len(news_list))))
� � � � � � � � date = mylist[y]
� � � � � � � � return '<center>%s</center><br>' % (date)
� � � � if x == 'news':
� � � � � � � � mylist = map(lambda i: news_list, filter(lambda i: i%2 == 1, range
(len(news_list))))
� � � � � � � � news = mylist[y]
� � � � � � � � return '%s<br>' % (news)
� � � � else:
� � � � � � � � return news_list

and call it with the follow syntax: news('[date/news]', [any number])

i keep getting "TypeError: list indices must be integers" on the
following line "date = mylist[y]"
can someone please explain this and give me the proper way to achieve
what im trying to do?


The code you posted assumes y is an integer. The TypeError message
shows that assumption is false. Nothing in this code will help you
resolve the problem, it's in the code that calls this code.

Throw a print type(y),y into tha start of the definition.
 
S

Steven D'Aprano

i keep getting "TypeError: list indices must be integers" on the
following line "date = mylist[y]"
can someone please explain this and give me the proper way to achieve
what im trying to do?

The obvious question is, what is the value of y?

Insert a line:

print type(y), y


immediately before date = mylist[y] and see what it says. I'm guessing
that it will say that y is a string.
 
J

John Machin

my head hurts from looking at it so long.
                mylist = map(lambda i: news_list, filter(lambda i: i%2 == 0, range(len(news_list))))
                date = mylist[y]


My head hurts from looking at that only briefly. Any good reason why
you wrote that in such a fashion instead of:
mylist = news_list[::2]
date = mylist[y]
or (even better):
date = news_list[y * 2]
?
 
D

dubux

thanks for help everyone. it turned out the function itself worked
fine.. it was the way i was calling it that was messing everything up.
i ended up re-doing the whole thing as follows, and it now works
perfectly.

def news(x,y):
news_file = '/home/scam/Desktop/www/info/news'
news = open(news_file, 'r')
news_list = news.readlines()
news.close()
if x == 'date':
mylist = map(lambda i: news_list, filter(lambda i: i%2 == 0, range
(len(news_list))))
date = mylist[y].replace("\n","")
return '<center><p>%s</p></center>\n\n' % (date)
if x == 'news':
mylist = map(lambda i: news_list, filter(lambda i: i%2 == 1, range
(len(news_list))))
news = mylist[y].replace("\n","")
return '<center><p>%s</p></center>\n<br>\n' % (news)
else:
return news_list

news_parse, count, news_list = " ", 0, news('list','list')
newss = map(lambda i: news_list, filter(lambda i: i%2 == 1, range
(len(news_list))))
while count < len(newss):
get_date = news('date', count)
get_news = news('news', count)
news_parse = '%s %s %s' % (news_parse, get_date, get_news)
count = count + 1
 
J

John Machin

thanks for help everyone. it turned out the function itself worked
fine.. it was the way i was calling it that was messing everything up.
i ended up re-doing the whole thing as follows, and it now works
perfectly.

def news(x,y):
        news_file = '/home/scam/Desktop/www/info/news'
        news = open(news_file, 'r')
        news_list = news.readlines()
        news.close()
        if x == 'date':
                mylist = map(lambda i: news_list, filter(lambda i: i%2 == 0, range
(len(news_list))))
                date = mylist[y].replace("\n","")
                return '<center><p>%s</p></center>\n\n' % (date)
        if x == 'news':
                mylist = map(lambda i: news_list, filter(lambda i: i%2 == 1, range
(len(news_list))))
                news = mylist[y].replace("\n","")
                return '<center><p>%s</p></center>\n<br>\n' % (news)
        else:
                return news_list

news_parse, count, news_list = " ", 0, news('list','list')
newss = map(lambda i: news_list, filter(lambda i: i%2 == 1, range
(len(news_list))))
while count < len(newss):
        get_date = news('date', count)
        get_news = news('news', count)
        news_parse = '%s %s %s' % (news_parse, get_date, get_news)
        count = count + 1


Unless I'm sorely mistaken, the whole of the above can be replaced by
something like (untested):

news_file = '/home/scam/Desktop/www/info/news'
news = open(news_file, 'r')
accum = []
for lineno, line in enumerate(news):
accum.append('<center><p>%s</p></center>\n%s\n' % (line.strip(),
('', '<br>')[lineno % 1]))
news_parse = ' '.join(accum)
news.close()
count = (lineno + 1) // 2 # if indeed count is needed

with the virtues of brevity and speed ... if there are N (date, news)
items, your code reads the file 2*N+1 times!!!

How big is N?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top