0

I have a function where the user passes in a file and a String and the code should get rid of the specificed delimeters. I am having trouble finishing the part where I loop through my code and get rid of each of the replacements. I will post the code down below

def forReader(filename):
try:
    # Opens up the file
    file = open(filename , "r")
    # Reads the lines in the file
    read = file.readlines()
    # closes the files
    file.close()
        # loops through the lines in the file
    for sentence in read:
            # will split each element by a spaace
            line = sentence.split()
    replacements = (',', '-', '!', '?' '(' ')' '<' ' = ' ';')
    # will loop through the space delimited line and get rid of
    # of the replacements
    for sentences in line:




# Exception thrown if File does not exist
except FileExistsError:
    print('File is not created yet')


forReader("mo.txt")

mo.txt

for ( int i;

After running the filemo.txt I would like for the output to look like this for int i

Mohamed Ali
  • 702
  • 8
  • 29

1 Answers1

1

Here's a way to do this using regex. First, we create a pattern consisting of all the delimiter characters, being careful to escape them, since several of those characters have special meaning in a regex. Then we can use re.sub to replace each delimiter with an empty string. This process can leave us with two or more adjacent spaces, which we then need to replace with a single space.

The Python re module allows us to compile patterns that are used frequently. Theoretically, this can make them more efficient, but it's a good idea to test such patterns against real data to see if it does actually help. :)

import re

delimiters = ',-!?()<=;'

# Make a pattern consisting of all the delimiters
pat = re.compile('|'.join(re.escape(c) for c in delimiters))

s = 'for ( int i;'

# Remove the delimiters
z = pat.sub('', s)

#Clean up any runs of 2 or more spaces
z = re.sub(r'\s{2,}', ' ', z)
print(z)

output

for int i
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • You are a life saver. If I have any more questions on this matter can I DM you? – Mohamed Ali Apr 29 '18 at 06:55
  • @jsilva Glad to help. If you have further questions on this particular code you can post a comment here. But if you have a new question that's not directly related to the above code, then just ask a fresh question. :) – PM 2Ring Apr 29 '18 at 07:16
  • Okay so now what I want to do is check if a particular word is in our new list after taking out the delimeters. How can I do that for example I tried to do a basic loop that goes through the new list and check for the word but no luck – Mohamed Ali Apr 29 '18 at 07:32
  • @jsilva That's a new question. :) But if you just want to test for a single word you can use the `in` operator to do that sort of thing. Eg, `s = 'this is a test sentence'; print('test' in s)`. However, that will also find partial words embedded inside other words, eg `'sent' in s` is True. And you may not want that. – PM 2Ring Apr 29 '18 at 07:39
  • @jsilva The safe way is to split your cleaned-up string into a list of words, and then you can safely use `in` with it. Eg, `'test' in s.split()` – PM 2Ring Apr 29 '18 at 07:44
  • well my plan is run this script against a java file so that for example if the java file has a for loop in it my script would say something like for loop starts here. then it would have like `i < 10` and say the variable i is less than 10. Its a parser to help those without programming knowledge understand code. I am doing a small part while my partner is working on some other parts. – Mohamed Ali Apr 29 '18 at 07:45
  • @jsilva It sounds like you want to be able to test your cleaned-up string against a list of keywords. There are lots of SO questions about that, eg https://stackoverflow.com/questions/3389574/check-if-multiple-strings-exist-in-another-string – PM 2Ring Apr 29 '18 at 07:50
  • thanks alot. I got alot of out of this conversation. Enjoy your day! – Mohamed Ali Apr 29 '18 at 07:52