Remove substring from each line of text file with regex

KideW

Text file (file.txt) looks like this:

First line.
2. Second line 
03 Third line
04. Fourth line
5. Line. 
6 Line

Desired output is 1) eliminating numbers at the beginning of line and 2) remove punctuation:

First line.
Second line
Third line
Fourth line
Line.
Line

I tried:

import re
file=open("file.txt").read().split()
print([i for i in file if re.sub("[0-9]\.*", "", i)])

But I get results only on word level instead of line level:

['First', 'line.', 'Second', 'line', 'Third', 'line', 'Fourth', 'line', 'Line.', 'Line']
Wiktor Stribiżew

You may fix your current code using

with open("file.txt") as f:
    for line in f:
        print(re.sub("^[0-9]+\.?\s*", "", line.rstrip("\n")))

See a Python demo.

You need to open a file and read it line by line. Then, ^[0-9]+\.?\s* pattern searches for 1 or more digits ([0-9]+) followed with an optional . (\.?) and then 0+ whitespaces (\s*) on each line and removes the match if found.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get a substring from each text line

remove current path portion from each line in a text file

Remove the last empty line from each text file

How to remove a pattern from each line in a text file in Python

How to remove multi-line blocks of text of varying sizes from a file given the first and last lines and a substring?

Remove the first part of each line of a text file

Using regex in perl to extract a substring, or line from a blob of text

Regex to remove line from css file

remove line comment from file by using regex

Remove newlines from each line of a text file that contains links and display those links in an iframe

How to remove domain part of each line from text file containing email addresses

How to remove similar elements from beginnign of each line in a text file using sed or similar?

Extract text from pattern for each line in a file

Creating files from each line of text in a file

Extract substrings from a text file on each line?

Remove style tag from a text file with regex

Using sed with regex to remove text from a file

How to remove a line from a text file?

Remove line breaks from text file

Remove duplicates line from text file (PHP)

Remove Duplicates On a Single Line From a Text File

Python - Read line from text file, update substring of line and write to new text file

Store each line of text file into an array and remove \n

How to write each values from Object to each line in a text file

Add each character from each line of a text file, to a list in Python

Atom editor regex expression to remove brackets from each time of text

How to strip a certain piece of text from each line of a text file?

PHP, cURL, strpos - Compare substring from text file line by line with string from variable

Remove remaining spaces in the line text file from bat file