How do I take a number associated with a word and have the word printed that number of times?
Question: I have a text file with a list of words with a number and want to alter the
list so instead of having the number next to the word, the word is printed
that number of times.

So for example, with this list:

    
    
    word, 2
    
    for, 3
    
    cat, 1
    
    dog, 2
    
    tiger, 1
    

I want it took look like this:

    
    
    word
    
    word
    
    for
    
    for
    
    for
    
    cat
    
    dog
    
    dog
    
    tiger
    

For my python program I have this so far:

    
    
    f = raw_input("Please enter a filename: ")
    def openfile(f):
        with open(f,'r') as a:
           a = a.readlines()
           b = [x.lower() for x in a]
           return b
    
    def fix(b):
        newlist = []
        for line in b:
            split_line = line.split(',')
    
    print openfile(f)
    

What I want to do is take the number and tell the program to print the word
that number of times and then delete the number but I am not sure how to do
that.

If you have any suggestions, answers, or need clarification please let me
know!

Thanks


Answer: If you want to change the file itself you can use
[fileinput.input](https://docs.python.org/2/library/fileinput.html#fileinput.input)
with `inplace=True` to change the file content:

    
    
    import fileinput
    import sys
    for line in fileinput.input("in.txt",inplace=True):
        if line.strip():
            w,i = line.split(",")
            sys.stdout.write("{}\n\n".format(w)*int(i))
    

Output:

    
    
    word
    
    word
    
    for
    
    for
    
    for
    
    cat
    
    dog
    
    dog
    
    tiger
    

