Search code examples
pythonunixcharactertext-filesunique

How to get all unique characters in a textfile? unix/python


Is there any other way of getting a list of all unique characters in a textfile?

I've tried the following way but is there a more pythonic way? Can the same output be achieved in unix command?

>>> import codecs
>>> from collections import Counter
>>> with codecs.open('my.txt','r','utf8') as fin:
...     x = Counter(fin.read())
...     print [i for i in x if len(i) == 1]
... 
[u'\u2014', u'\u2018', u'\u201c', u' ', u'\xa3', u'$', u'(', u',', u'0', u'4', u'8', u'@', u'D', u'H', u'L', u'P', u'T', u'\xd7', u'X', u'`', u'd', u'h', u'l', u'p', u't', u'x', u'\ufffd', u'\ufeff', u'\u2013', u'#', u"'", u'+', u'/', u'3', u'7', u';', u'?', u'C', u'G', u'K', u'O', u'S', u'W', u'_', u'\xe0', u'c', u'g', u'k', u'\u2026', u'o', u's', u'w', u'\n', u'"', u'&', u'*', u'\xad', u'.', u'2', u'6', u':', u'>', u'B', u'F', u'J', u'N', u'R', u'V', u'Z', u'b', u'f', u'\xe9', u'j', u'n', u'r', u'v', u'z', u'\t', u'\u2019', u'\u201d', u'!', u'%', u')', u'-', u'1', u'5', u'9', u'=', u'A', u'\xc2', u'E', u'I', u'M', u'Q', u'U', u'Y', u'a', u'\xe2', u'e', u'i', u'm', u'q', u'u', u'y']

Solution

  • One way is to use sets:

    fh = open('my.txt','r').read()
    unique_chars = set(fh)
    len(unique_chars) #for the length.