Home > Mobile >  If the first 3 characters are the same, delete the line in a text file?
If the first 3 characters are the same, delete the line in a text file?

Time:05-08

Goal: Open the text file. Check whether the first 3 characters of each line are the same in subsequent lines. If yes, delete the bottom one.

The contents of the text file:

cat1
dog4
cat3
fish
dog8

Desired output:

cat1
dog4
fish

Attempt at code:

line = open("text.txt", "r")
for num in line.readlines(): 
    a = line[num][0:3] #getting first 3 characters

    for num2 in line.readlines():
        b = line[num2][0:3]
        
        if a in b:
            line[num2] = ""

CodePudding user response:

You can use a dictionary to store the first 3 char and then check while reading. Sample check then code below

line = open("text.txt", "r")
first_three_char_dict = {}
for num in line.readlines():
    a = line[num][0:3]  # getting first 3 characters

    if first_three_char_dict.get(a):
        line[num] = ""
    else:
        first_three_char_dict[a] = num
        pass;

CodePudding user response:

try to read line and add the word (first 3 char)into a dict. The key of dict would be the first 3 char of word and value would be the word itself. At the end you will have dict keys which are unique and their values are your desired result.

CodePudding user response:

Open the file and read one line at a time. Note the first 3 characters (prefix). Check if the prefix has been previously observed. If not, keep that line and add the prefix to a set. For example:

with open('text.txt') as infile:
    out_lines = []
    prefixes = set()
    for line in map(str.strip, infile):
        if not (prefix := line[:3]) in prefixes:
            out_lines.append(line)
            prefixes.add(prefix)
    print(out_lines)

Output:

['cat1', 'dog4', 'fish']

Note:

Requires Python 3.8

CodePudding user response:

You just need to check if the data is already exist or not in temporary list

line = open("text.txt", "r")
result = []
for num in line.readlines():
    data = line[num][0:3]  # getting first 3 characters

    if data not in result: # check if the data is already exist in list or not
        result.append(data) # if the data is not exist in list just append it

  • Related