[英]comparing lists to columns in a txt file
我正在嘗試在python中讀取文件的第5列。 如果第三列匹配“ Plop”,則將第三列存儲在list1中;否則,如果第五列匹配“ toto”,則將第三列存儲在list2中。 然后,我要做的就是檢查兩個列表中每個元素的長度。 當我運行代碼時,出現以下錯誤:
if word1 in columns[:4]
^
SyntaxError: invalid syntax
我的代碼如下:
word1=['Popo']
word2=['toto']
aList1 = list()
aList = list()
for line in open("test.txt"):
columns = line.split(" ")
#columns.lookup([toto])
if word1 in (columns[:4]):
aList1.insert(columns[:2])
if word2 in (columns[:4]):
aList.insert(columns[:2])
#print '\n'.join(aList1)
for entry in aList1:
try:
l = len(entry)
print "Length of", entry, "is", l
except:
print "Element", entry, "has no defined length"
for entry in aList:
try:
l = len(entry)
print "Length of", entry, "is", l
except:
print "Element", entry, "has no defined length"
您到底缺少了: ,應該是:
if word1 in columns[:4]:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.