Dec-13-2021, 03:38 AM
(Dec-13-2021, 01:01 AM)menator01 Wrote: Maybe something like this:
#! /usr/bin/env python3 import re file = 'some.txt' mylist = [] with open(file, 'r') as lines: print('----Original----') for line in lines: print(line.strip()) new_line = re.sub('whiff', 'woo', line) mylist.append(new_line) with open(file, 'w') as lines: for line in mylist: lines.write(f'{line.strip()}\n') with open(file, 'r') as lines: print() print('----New File----') for line in lines: print(line.strip())
Output:----Original---- yippe wahoo whiffenhard yerp ----New File---- yippe wahoo wooenhard yerp
this is excellent thank you. i forgot to mention i am trying to replace the entire line as in replace "whiffenhard" with "woo" but i can likely play with what you gave me.