-2

I have tab occured at the beginning of a string in a list, I'm uploading. I've tried all the methods I've found to remove it, but it doesn't work

Actually you can see what I've tried in the code itself. Nothing works

# 2D List I got [['12/20', 'Instrument', 'Maintainance', '\tDeb', 'Pb', 'Tool', '5']]
# \tDeb - is the one with tab, I want to remove
import re
for line in list_2D:
    for piece in line:
        re.sub(r"[\n\t\s]*", ' ', piece)
        piece.replace("\t","")
        piece.replace("\\t","")
print(list_2D[0][3])
# Result -> "   Deb"

As a result I want to get any string with similar issue without that tab.

WGS
  • 13,969
  • 4
  • 48
  • 51
mrIden
  • 15
  • 1
  • 3

1 Answers1

0

If you don't care about trailing whitespace you can use piece.strip() to remove all enclosing whitespace.

Alternately you can do something like re.sub("^\s+", '', piece)

Gabe Koss
  • 51
  • 1
  • 4
  • Just tried it and still doesn't work 'import re piece = "\tDeb" if "\t" in piece: print("Working on piece:", piece) print('"\\t" in piece:',"\t" in piece) piece.replace("\\", "") re.sub("\t", '', piece) re.sub("^\s+", '', piece) print(piece)' here's what I get Working on piece: Deb "\t" in piece: True Deb It still exists – mrIden Jan 24 '19 at 20:13