I'm trying to read the 5th column of a file in python. If the third column matches "Plop" I store the third column in a list1, otherwise if the fifth column matches "toto" I store the third column in a list2. Then all I do is I check the lenght of every element in both lists. When I run my code I get this error:
if word1 in columns[:4]
^
SyntaxError: invalid syntax
My code is as follow:
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]:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.