Reputation: 309
I have a file of nearly 1500 lines that contains symbols like ")(()(&&^%&^a%&#@%^%*&^" alongwith some two or three alphabets in the entire file.
How can i search for these alphabets in such file and display the found alphabets on the o/p screen.
Upvotes: 1
Views: 104
Reputation: 7821
Building on Tim's answer, you can use this code to save some memory.
import re
alphas = []
with open("giantfile.txt") as infile:
for row in infile:
alphas.extend(re.findall("[A-Za-z]+", row))
print alphas
Given this input file:
aaa
bbb
c12d
The output would be
['aaa', 'bbb', 'c', 'd']
Upvotes: 2
Reputation: 336158
Probably the fastest way would be to do
import re
with open("giantfile.txt") as infile:
print(re.findall("[A-Za-z]+", infile.read()))
Upvotes: 8