Search code examples
pythonstringscramble

Scrambling of letters within a text file


I am preparing a test data which must have different letters say அ-20 times ம-30 times , த-40 times .....( They are UTF-8 coding supported Tamil Languague letters ) this could be achieved using a print statement

{print ( ' ம் ' * 30 ) + ( ' த ' * 40  ) + }

But , I need to scramble them so that they dont appear in any particular order . I have around 230+ letters which I would be printing for 20,30,40 times . And then I need to scramble them and write them to an output file . Any help in this regard be helpful .


Solution

  • Credits to my friend @AswinMurugesh who helped me with the code .

    The following code did the trick .

    import codecs
    import tamil
    from random import shuffle
    
    inp_file = codecs.open("/home/sibi/Desktop/scramble.txt",encoding="utf-8")
    inp_text = inp_file.read().rstrip()
    
    tamil_letters = tamil.utf8.get_letters(inp_text)
    shuffle(tamil_letters)
    tamil_letters = "".join(tamil_letters).encode("utf-8")
    print tamil_letters
    
    out_file = open('outputscrambled.txt','w')
    out_file.write(tamil_letters)