简体   繁体   中英

Remove two first character of line if match (Python)

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

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)

Added note after comments from @kng: you can use the additional condition line[6],= ',' to avoid removing the '11' when there are only 6 characters before the comma.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM