0

For example I've got a file with lines like this

\tline1\t
\t\tline2\t
\t\tline3
\tline4

I need to remove only first tab in the beginning of every string(and I don't care if there are more tabs in the line) So the result suppose to look like this

line1\t
\tline2\t
\tline3
line4

How to do it?

0

2 Answers 2

3
s = "\thello"
s.replace("\t", "", 1)

Unsure if it's needed but this will handle stuff like `"hello\tworld" also, i.e. replace the first tab in the string disregarding where in the string it is

Sign up to request clarification or add additional context in comments.

Comments

2

Regular expressions may help:

>>> import re
>>> pattern = re.compile('^\t')  # match a tab in the beginning of the line
>>> pattern.sub('', '\tline1\t')
'line1\t'
>>> pattern.sub('', '\t\tline2\t')
'\tline2\t'
>>> pattern.sub('', 'line3\t')
'line3\t'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.