Home > OS >  How to delete a range of characters from every line from a string?
How to delete a range of characters from every line from a string?

Time:01-08

I have the following string and I would like to remove every 6th to 11th character for every line.

string = ('''

00:00:100:00:00

00:00.711,00:00

00:00,2()!00:00

''')

So far I was only able to remove the characters from the 1st line. How do I do it to repeat it for every sigle line?

Did:

x = string[6:11]

print(string.replace(x, ''))

Got:

00:0000:00

00:00.711,00:00

00:00,2()!00:00

Want:

00:0000:00

00:0000:00

00:0000:00

CodePudding user response:

split the string on newlines, take the desired substring of each line, and then join it back:

>>> string = ('''
...
... 00:00:100:00:00
...
... 00:00.711,00:00
...
... 00:00,2()!00:00
...
... ''')
>>> print('\n'.join(line[:5]   line[10:] for line in string.split('\n')))


00:0000:00

00:0000:00

00:0000:00

CodePudding user response:

You could reformat the entry string and split it, then only take the first 5 and last 5 characters:

string = (''' 00:00:100:00:00 00:00.711,00:00 00:00,2()!00:00 ''')
elements = string.strip().split()
[element[:5] element[-5:] for element in elements]
# ['00:0000:00', '00:0000:00', '00:0000:00']

CodePudding user response:

Firstly, the string needs to be split into a list using lst = string.split("\n"). Then, we need to loop through the elements in this list, using a for loop.

Now, their are two main routes:

The first way is using new_lst.append(i[:5] i[10:]). This basically takes the first 5 elements of the line, and combines it with every element after the 10th.

The other way, which is commented in the code below, is new_lst.append(i.replace(str(i[5:10]), "")) This is what you were doing, where you take the 5th to 10th characters and you search for them in the string and replace them with "", which is nothing.

Both ways work, although the first is slightly better.

Here is the code:

string = ('''

00:00:100:00:00

00:00.711,00:00

00:00,2()!00:00

''')

lst = string.split("\n")
new_lst = []
for i in lst:
    new_lst.append(i[:5] i[10:])
    #new_lst.append(i.replace(str(i[5:10]), ""))

string = "\n".join(new_lst)
print(string)

CodePudding user response:

A string in Python has a splitlines method. Combined with a list comprehension and some slicing this is pretty straightforward.

>>> string = ('''
... 
... 00:00:100:00:00
... 
... 00:00.711,00:00
... 
... 00:00,2()!00:00
... 
... ''')
>>> string.splitlines()
['', '', '00:00:100:00:00', '', '00:00.711,00:00', '', '00:00,2()!00:00', '']
>>> [line[:6]   line [11:] for line in string.splitlines() if line != '']
['00:00:0:00', '00:00.0:00', '00:00,0:00']

Though it appears that your requirement of removing the 6th through 11th characters doesn't actually correspond to your expected output.

If we shift the bounds by one, we can get your expected output.

>>> [line[:5]   line [10:] for line in string.splitlines() if line != '']
['00:0000:00', '00:0000:00', '00:0000:00']
  • Related