python - How do I take a number associated with a word and have the word printed that number of times? -
i have text file list of words number , want alter list instead of having number next word, word printed number of times.
so example, list:
word, 2 for, 3 cat, 1 dog, 2 tiger, 1
i want took this:
word word cat dog dog tiger
for python program have far:
f = raw_input("please enter filename: ") def openfile(f): open(f,'r') a: = a.readlines() b = [x.lower() x in a] return b def fix(b): newlist = [] line in b: split_line = line.split(',') print openfile(f)
what want take number , tell program print word number of times , delete number not sure how that.
if have suggestions, answers, or need clarification please let me know!
thanks
if want change file can use fileinput.input inplace=true
change file content:
import fileinput import sys 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 cat dog dog tiger
Comments
Post a Comment