开发者

python 2.7, using Counter on open('file.txt')

I got a good answer before for a similar problem b开发者_JAVA技巧ut 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.


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜