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

Popular posts from this blog

php - Autoloader issue not returning Class -

java - How to put two numbers separated by a space into two different arrays -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -