Hi, I have a problem. I basically have this python code, that generates a file.
I'm trying to get it to output a html file with a chosen name (chosencategory) but the problem is the chosencategory variables are already defined so it attempts to name the html file the contents of the variable.
What i'm basically trying to do is extract the literal name of the variable, and not the contents. Is there any way to do this?
Is there anyway I can do this? btw I'm a programming noob.
output_file = file(filename, 'w')
def build_html(chosenCategory):
#my attempt, which returned an error
filename = '%s.html' %(chosenCategory)
print chosenCategory
example of the variable's output (chosencategory output)
>>> print sports
(('AFL', 6L), ('American Football', 2L), ('Backgammon', 1L), ('Badminton', 2L), ('Baseball', 2L), ('Basketball', 10L), ('Bowling', 1L), ('Chess', 1L), ('Climbing', 1L), ('Cricket', 11L), ('Cross Country', 1L), ('Cycling', 2L), ('Dancing', 1L), ('Darts', 1L), ('Dodgeball', 1L), ('Dog Racing', 1L), ('Dog Sledding', 1L), ('Drifting', 1L), ('Extreme Ironing', 1L), ('Fencing', 1L), ('Fishing', 1L), ('Frisbee', 1L), ('Golf', 5L), ('Hacky-Sack', 1L), ('Hockey', 1L), ('Horse Riding', 1L), ('Ice Hockey', 1L), ('Jogging', 1L), ('Karate', 3L), ('MMA', 1L), ('Modern Pentahlon', 1L), ('Moto GP', 1L), ('Motocross', 2L), ('Mountain Climbing', 1L), ('Netball', 1L), ('Pool', 1L), ('Racing', 1L), ('Rugby League', 15L), ('Rugby Union', 6L), ('Running', 2L), ('Skateboarding', 2L), ('Skiing', 2L), ('Snooker', 1L), ('Snowboarding', 2L), ('Soccer', 27L), ('Softball', 1L), ('Surfing', 1L), ('Swimming', 4L), ('Table Tennis', 3L), ('Tennis', 13L), ('Touch Football', 1L), ('Track and Field', 1L), ('Trekking', 1L), ('Volleyball', 5L), ('Water Polo', 1L), ('White Water Rafting', 1L), ('Wii Sports', 1L), ('World Superbikes', 1L))
the error i get:
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
build_html(sports)
File "C:/Documents and Settings/Administrator/Desktop/New Folder/popularity3.py", line 26, in build_html
filename = '%s.html' %(chosenCategory)
TypeError: not all arguments converted during string formatting
I understand this error, it's because it's trying to use all the contents of the variable (above) to create the html file. I just want the file to be called sports.html :( Thanks for the help guys