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

Call the function this way:
build_html('sports')

Call the function this way:
build_html('sports')

Thank you so much for the suggestion, but the problem is it needs to be called as a variable because it's variable properties are used in another part of the program.
Is there a way to extract it as a string in the
define function area?
or maybe something involving a dictionary?
The annoying bit is i actually completed the program, I just can't get it to output the files with the correct name.

ok to be more clear, I want the variable sports to still print the data in the sports variable, but I need to also use the name 'sports' as a string to name the html file I create. The problem is I can't just hardcode it in, because there are 6 different categories. :(

You can not do it in the function you are calling, because the variable is now chosenCategory and the function does not have the information that this variable used to be called sports or whatever.

So my suggestion is to make the category string the first item in the tuple and then use a slice of the tuple for normal use ...

def build_html(chosenCategory):
    filename = '%s.html' % (chosenCategory[0])     
    return filename

# test
# make the category string the first item in the tuple
sports = ('sports', ('AFL', 6L), ('American Football', 2L))

fname = build_html(sports)

print fname  # sports.html

# to use the tuple for its values slice out the category name
print sports[1:]  # (('AFL', 6L), ('American Football', 2L))
commented: Worked perfectly :) +0

This is a very hacked solution but:

>>> sports = ## Defined as above - snipped for length
>>> def build_html(chosenCategory):
...     up_locals = sys._getframe(1).f_locals
...     for each_var in up_locals:
...         if id(up_locals[each_var]) == id(chosenCategory):
...             var_name = each_var
...     filename = '%s.html' % (var_name)
...     print filename
...     
>>> build_html(sports)
sports.html
>>>

You could even be brazen and shorten that for loop to a single list comprehension (albeit a very long one):

def build_html(chosenCategory):
    var_name = [each_var for each_var in sys._getframe(1).f_locals if id(sys._getframe(1).f_locals[each_var]) == id(chosenCategory)][0]
    filename = '%s.html' % (var_name)
    print filename
commented: actually a good hack +9
commented: Very Helpful! solved my problem. +0

Thank you , both suggestions worked perfectly :) The program was running perfectly!
My problem now is I realised I actually needed a string input. e.g. ('sports') So now I need the complete opposite.
Is there a way to use a string to call a previously named variable? Thank you so much
i've been researching and something like exec or .append or vars() might be what i'm looking for.. what do you think??

Is there a way to use a string to call a previously named variable?

Eval is what you're looking for:

>>> sports = [1,23,4]
>>> eval( 'sports' )
[1, 23, 4]
>>>

Thanks, the eval function actually worked. But I actually solved the problem like an hour ago, by rewriting my program using a dictionary.
To help anyone else that might be stuck on this in the future,
what I did was use a dictionary with all the variables, which is more powerful than variable apparently? So it allowed me to take a string from the user input and output the data.
But yeah, eval also works, which I had seen it sooner lol. Will use it next time. But yeah, thank you Vegaseat and Jlm699, I appreciate the help so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.