如何將字符與制表符 \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]
這和 \t, '\t' 不起作用,SyntaxError: invalid syntax (, line 41) 我檔案中的每一行都是用一個英語單詞寫的,然后是一個越南語單詞,比如
Word t?:第 999 行
我的目標是洗掉每個英文單詞
uj5u.com熱心網友回復:
您有語法錯誤。一個有效的版本可能是:
for word in list_of_words:
if word[0] != '\t': # '\t' is a literal tab character
word[0] = word[1]
但我覺得也許你想從每一行中去掉前導制表符或空格。如果是這樣,那么我們可以使用re.sub:
for word in list_of_words:
word = re.sub(r'^\s ', '', word)
# do something with the word
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/442835.html
標籤:python-3.x
