Counting how many words each line has in at text file with Python (using str.split) -
i have 2 files, 1 input "our_input.txt" (same directory code file), oscar wild's dorian gray. anyway, there's output file, , need open original file, , let python count how many words each line has, , write in output.
i tried, got lost...
you can try this.
first read input file:
with open('our_input.txt') f: lines = f.readlines()
then count number of words per line , write output file:
with open('our_output.txt', 'w') f: index, value in enumerate(lines): number_of_words = len(value.split()) f.write('line number {} has {} words.\n'.format(index + 1, number_of_words))
Comments
Post a Comment