codehaus


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to remove a string from a txt file?


On Wed, Sep 4, 2019 at 11:11 AM Spencer Du <spencerdu at hotmail.co.uk> wrote:
>
> Hi
>
> I want to remove a string from a txt file and then print out what I have removed. How do I do this.
>
> The txt file is in this format and should be kept in this format.
>
> txt.txt:
> laser,cameras,
>
> Thanks
> --
> https://mail.python.org/mailman/listinfo/python-list

read the file line by line.  if it doesn't contain your string, write
it to a new file.  for each line, use string functions to find the
string you want to remove.  One way is to split on ',' or use
csvreader to get a list of words between comma.  Print the one you
like and join the values on each side of the text you are looking for.
Write that to a file

Lather, rinse and repeat

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays