Home > front end >  comparing characters to tabulation in words of a list
comparing characters to tabulation in words of a list

Time:03-13

How can i compare a character with the tab \t ?

lines = trente_words
list_of_words = lines.splitlines()
print(list_of_words)

for word in list_of_words:
    if word[0] != '\\t'
    word[0] = word[1]

This, and \t, '\t', do not work, SyntaxError: invalid syntax (, line 41) Every line in my file is written with an English word followed by a Vietnamese word like
Word từ : 999th line
my goal is to remove each English word

CodePudding user response:

You have syntax errors. A valid version might be:

for word in list_of_words:
    if word[0] != '\t':     # '\t' is a literal tab character
        word[0] = word[1]

But I sense that maybe you want to strip leading tabs or whitespace from each line. If so, then we can use re.sub for that:

for word in list_of_words:
    word = re.sub(r'^\s ', '', word)
    # do something with the word
  • Related