Python remove word containing "l"

colin

I'm currently working on a small program.

The aim of the program is to take an input from a file, edit the file to remove any word containing the letter "l" and then output this into an output file.

The code I have at the moment works, however, it does not remove the word containing the letter "l" just the letter itself.

Here is my code

def my_main(ifile_name, ofile_name):
    ifile_name = open(ifile_name, 'r')
    ofile_name = open(ofile_name, "w+")
    delete_list = ['l']
    for line in ifile_name:
        for word in delete_list:
            line = line.replace(word, "")
        ofile_name.write(line)
    ifile_name.close()
    ofile_name.close()

Thank you

UPDATE

This is what the input file looks like:

The first line never changes. 
The second line was a bit much longer. 
The third line was short. 
The fourth line was nearly the longer line. 
The fifth was tiny. 
The sixth line is just one line more.
The seventh line was the last line of the original file.

And the output file should look like the following when the code is correct

The first never changes. 
The second was a bit much. 
The third was short. 
The fourth was the. 
The fifth was tiny. 
The sixth is just one more.
The seventh was the of the.
John Ruddell

without seeing what your file is like its hard to tell what exactly to use so if you could update the question that would be great

but currently you are looping through each letter instead of the words... use split() to split the words into a list and change that list then rejoin the words together to have a string without the words that contain your letter

words = ''
with open(ifile_name,"r") as file:
    for line in file:
        list_of_words = line.split(' ')
        for key, word in enumerate(list_of_words):
            if 'l' in word:
                list_of_words[key] = ''

        words += ' '.join(w for w in list_of_words if w != '')
        words += '\n'

with open(ofile_name, "w+") as file:
    file.write(words)

the nice thing with this is you dont have any issues with white space. you will get a regular string with single spaces

EDIT: as pointed out in the comments a better way to do this (non in memory one for the whole file) is to do it inline

with open(ifile_name,"r") as in_file, open(ofile_name, "w+") as out_file:
    for line in file:
        list_of_words = line.split(' ')
        for key, word in enumerate(list_of_words):
            if 'l' in word:
                list_of_words[key] = ''

        out_file.write(' '.join(w for w in list_of_words if w != ''))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

python: remove lines containing a word in a list

From Dev

Remove all lines containing word NOT in dictionary

From Dev

Grep command, to remove lines containing a specified word

From Dev

Remove line from file if containing word from another .txt file in python/bash

From Dev

Remove word extension in python

From Dev

Remove word extension in python

From Dev

Remove <img> tag containing a word from Html string using PHP

From Dev

Remove all images in a table, based on string containing a word on a href tag

From Dev

Remove elements in an array containing a certain word/string in a single line of statement

From Dev

How do I remove a line containing word in multiple files?

From Dev

How to remove particular word if containing that character and store in another variable in php

From Dev

How to remove character containing String from Python?

From Dev

Remove a tuple containing nan in list of tuples -- Python

From Dev

Remove a tuple containing nan in list of tuples -- Python

From Dev

Python Regex - remove words containing ":" from file

From Dev

Extract a Sentence Containing a Word Using Python... As well as the sentences around it?

From Dev

How to find URL containing one word AND another using re in Python?

From Dev

Extract a Sentence Containing a Word Using Python... As well as the sentences around it?

From Dev

Python - Finding the top 5 rows containing a word in a dataframe

From Dev

Python, Remove word start with specific character

From Dev

Remove word in a list from two columns if the word is in both columns in Python

From Dev

sed remove all lines containing the word password.But don't keep the empty line

From Dev

How to remove a line containing non-ascii values in python

From Dev

Python: How to remove a list containing Nones from a list of lists?

From Dev

Python: Remove URL from string, URL containing backslashes

From Dev

Regex: Match word not containing

From Dev

Count lines containing word

From Dev

Count lines containing word

From Dev

Regex containing a specific word

Related Related

  1. 1

    python: remove lines containing a word in a list

  2. 2

    Remove all lines containing word NOT in dictionary

  3. 3

    Grep command, to remove lines containing a specified word

  4. 4

    Remove line from file if containing word from another .txt file in python/bash

  5. 5

    Remove word extension in python

  6. 6

    Remove word extension in python

  7. 7

    Remove <img> tag containing a word from Html string using PHP

  8. 8

    Remove all images in a table, based on string containing a word on a href tag

  9. 9

    Remove elements in an array containing a certain word/string in a single line of statement

  10. 10

    How do I remove a line containing word in multiple files?

  11. 11

    How to remove particular word if containing that character and store in another variable in php

  12. 12

    How to remove character containing String from Python?

  13. 13

    Remove a tuple containing nan in list of tuples -- Python

  14. 14

    Remove a tuple containing nan in list of tuples -- Python

  15. 15

    Python Regex - remove words containing ":" from file

  16. 16

    Extract a Sentence Containing a Word Using Python... As well as the sentences around it?

  17. 17

    How to find URL containing one word AND another using re in Python?

  18. 18

    Extract a Sentence Containing a Word Using Python... As well as the sentences around it?

  19. 19

    Python - Finding the top 5 rows containing a word in a dataframe

  20. 20

    Python, Remove word start with specific character

  21. 21

    Remove word in a list from two columns if the word is in both columns in Python

  22. 22

    sed remove all lines containing the word password.But don't keep the empty line

  23. 23

    How to remove a line containing non-ascii values in python

  24. 24

    Python: How to remove a list containing Nones from a list of lists?

  25. 25

    Python: Remove URL from string, URL containing backslashes

  26. 26

    Regex: Match word not containing

  27. 27

    Count lines containing word

  28. 28

    Count lines containing word

  29. 29

    Regex containing a specific word

HotTag

Archive