0

So we were given an assignment to create a code that would sort through a long message filled with special characters (ie. [,{,%,$,*) with only a few alphabet characters throughout the entire thing to make a special message.

I've been searching on this site for a while and haven't found anything specific enough that would work.

I put the text file into a pastebin if you want to see it

https://pastebin.com/48BTWB3B

Anywho, this is what I've come up with for code so far

code = open('code.txt', 'r')
lettersList = code.readlines()
lettersList.sort()

for letters in lettersList:
    print(letters)

It prints the code.txt out but into short lists, essentially cutting it into smaller pieces. I want it to find and sort out the alphabet characters into a list and print the decoded message.

stripbubbles
  • 1
  • 1
  • 1

1 Answers1

0

This is something you can do pretty easily with regex.

import re
with open('code.txt', 'r') as filehandle:
    contents = filehandle.read()
letters = re.findall("[a-zA-Z]+", contents)

if you want to condense the list into a single string, you can use a join:

single_str = ''.join(letters)
Adam Holloway
  • 413
  • 4
  • 7