Home > Software design >  Replacing words from a file to a new file using for loops
Replacing words from a file to a new file using for loops

Time:05-13

I am trying to create a program that takes in a text file and replaces each word that does not start with a vowel with the same word with a dash " " in front of it. However, every time I run the program removes random areas of the text file.

Example input text file:

This is a text file. amazing words should be removed

Example output file:

This is a text file . amazing words should be removed

fin = open("file.txt", "r")
fout = open("new.txt", "w")
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U',]


for line in fin:
    for word in fin:
        if word[0] not in vowels:
            word = word   " "
            fout.write(line.replace(line, word))

fin.close()
fout.close()

CodePudding user response:

Please see the inline notes for explanation.

fin = open("file.txt", "r").read().split()  # read contents in and split them into words
fout = open("new.txt", "wt")
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U',]

words = []
for word in fin:  # for each word 
    if word[0] not in vowels:  # if it doesnt starts with vowel
        word  = " "  # add a plus operator
        words.append(word)  # append to list of words
    else:
        words.append(word)  # if it does start with vowel append unchanged word to list of words
fout.write(' '.join(words))  # write the list of words joined by spaces to file
fout.close()

Some extra tips:

using the with statement when dealing with files is a highly recommended approach, for example.

textfile = open(somefile,"rt") # instead of this
text = textfile.read()

with open(somefile, "rt") as textfile:
    text = textfile.read()
    '... do something'
'... do something'  # when you exit the with statement the file gets closed automatically.

CodePudding user response:

vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']

with open("file.txt", "r") as fin, open("new.txt", "w") as fout:
    lines = [x.split() for x in fin.readlines()]
    lines = [' '.join(x if x[0] in vowels else f'{x} ' for x in line) for line in lines]
    fout.writelines(lines)

test = ['This is a text file.', 'amazing words should be removed']
lines = [x.split() for x in test]
test = [' '.join(x if x[0] in vowels else f'{x} ' for x in line) for line in lines]
print(test)

Output: To correct the file. edge case would involve a more in depth program...

['This  is a text  file. ', 'amazing words  should  be  removed ']

CodePudding user response:

Do that and will work just like you want:

# import re module

# re module provides support
# for regular expressions
import re

# Make a regular expression
# to find string starting with vowel
regex = '^[aeiouAEIOU][A-Za-z0-9_]*'

f = open(input.txt, 'r ') # open the txt file

# divide the text in lines and the lines in words

with open(txt, 'r') as fp:
  lines = fp.readlines() #reading line by line
    for i in range(0, len(lines)):
       words = lines[i].split()
       for w in words : #reading word for word
           if(re.search(regex, w)): #find the words with vowel
              new_word = f'{w}  '
              lines[i] =lines[i].replace(w, new_word) 

with open('output.txt', 'w') as f:
    for line in lines:
      f.write(line)
      f.write('\n')

Done!

  • Related