0

How to count no of words line by line in Python?

I wrote below code - file = open("newfile.txt", "w") file.write ("" "abc abhi deh kjhkl Abh abc ahjk akls Kllls shift abc abc def" "") file.close() filename = "newfile.txt" import re count = 0 with open(filename) as f: for line in f: if "abc" in line" count += len(re.findall(r"abc", line)) print count This code is working. But when I replace file with a zipped file, it doesn't work. The only change in case of zipped would be import gzip filename =gzip.open("file-name") Pls correct me.

14th Jun 2017, 11:41 AM
Abhishek Dubey
Abhishek Dubey - avatar
1 Odpowiedź
14th Jun 2017, 11:59 AM
Madera Oxidada
Madera Oxidada - avatar