Home > other >  Output lines while deleting specific characters in Python
Output lines while deleting specific characters in Python

Time:03-21

I have a text file which consists of lines of the following general format:

5.1: 23
10.145: 34

etc.

I need Python code that outputs the content of each line such that the characters after the period . and before the colon : are deleted.

That is, it would output the above as

5: 23
10: 34

So far I have the following code that I have been using for similar edits to the text file:

with open("file.txt", "r") as f:
    with open("newfile.txt", "w") as output: 
        for line in f:
             output.write(line[1:])

which I used to delete the first character of each line. How can I do what I need here?

CodePudding user response:

You could try regular expressions:

import re
txt="10.145: 34"
re.sub("\.\d :",":",txt)
  • ".\d :" matches period, one or more digits after the period and colon.
  • ":" this replaces the match.

Here is the full program.

import re
import os, sys

output_file_name = 'results.txt'
if not os.path.exists(output_file_name):
    out = open(output_file_name, 'w ')       
else:
    print('File already exists!')
    sys.exit()

with open("file.txt", "r") as f:
    for line in f:
        new_line = re.sub("\.\d :",":",line)
        print(new_line, end="") #this will print to screen
        out.write(new_line) #this will write to output file

CodePudding user response:

You can use regular expressions for this:

Example general code:

import re

x = ['5.1: 23', '10.145: 34']


def my_sub(x, delete_string = "\..*:", replacement = ":"):
    return re.sub(delete_string, replacement , x)


list(map(my_sub, x))

For your specific case:

with open("file.txt", "r") as f:
    with open("newfile.txt", "w") as output: 
        for line in f:
            output.write(my_sub(line))

Explaining the regex:

  • \. is escaping the special character '.' to make it the normal character '.'
  • '.' is the special character wildcard
  • '*' means match as many as you can
  • ':' we go until we hit this
  • Related