Ad

Python 2.7, Using Counter On Open('file.txt')

- 1 answer

I got a good answer before for a similar problem but I didnt consider the scalability of the question that I asked. I had trouble with the text editor I am using concerning pasting in large amounts of text and ending up with either newlines, or if I remove all the newlines from the document then the text goes off the screen and wont allow me to scroll to the end. So I saw how to use open on the text as a file but now the code wont work properly.
Here is the code:

import sys 
import os
from collections import Counter
def main():
    with open('garbledText.txt') as text:
        print [k for k,v in Counter(text).items() if v<3]
if __name__=='__main__':
    main()

it seemed like it was going in the right direction because if I change 'v<3' to 'v<1' I get an empty list, but with 'v<3' I get all the chars.
what I am trying to do is parse the 'garbledText.txt' to find chars that appear 1-2 times.

Ad

Answer

replace text for text.read(), the first make a collection of lines and the second of caracters.

from collections import Counter

def main():
    with open('garbledText.txt') as text:
        collection = Counter(text.read())
    print [char for char, times in collection.items() if times < 3]

if __name__=='__main__':
    main()
Ad
source: stackoverflow.com
Ad