I have a text file large with content format below, i want remove two first character 11, i try to search by dont know how to continue with my code. Looking for help. Thanks
file.txt
11112345,67890,12345
115432,a123q,hs1230
11s1a123,qw321,98765321
342342,121sa,12123243
11023456,sa123,d32acas2
My code
import re
with open('in.txt') as oldfile, open('out.txt', 'w') as newfile:
for line in oldfile:
removed = re.sub(r'11', '', line[:2]):
newfile.write(removed)
Result expected:
112345,67890,12345
115432,a123q,hs1230
s1a123,qw321,98765321
342342,121sa,12123243
023456,sa123,d32acas2
CodePudding user response:
Here is a suggestion of easy-to-read solution, without using regex that I find a bit cumbersome here (but this is obviously a personal opinion):
with open('in.txt', 'r') as oldfile, open('out.txt', 'w') as newfile:
for line in oldfile:
newfile.write(line[2:] if line.startswith('11') else line)
CodePudding user response:
The main issue in your code is this line :
removed = re.sub(r'11', '', line[:2]):
Because in this case :
- You'll only write the 2 first character of the line in the file
- You'll replace each "11" by empty char
The answer provided above is great, even if it doesn't match you're 2 expectation (115432,a123q,hs1230)
However, if you definitely wants to use the regex :
removed = re.sub(r'^(11)','', line)
This line should work