Reputation: 13395
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?
Upvotes: 0
Views: 691
Reputation: 34282
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'
Upvotes: 2
Reputation: 1693
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
Upvotes: 3