newbie question,I have the following code where I need to match the data in variable gerrit with the list gerrit_refs and get the corresponding match and save in a variable,how can I do that?
'''
gerrit_refs:
refs/changes/89/202089/4
refs/changes/39/205739/2
refs/changes/94/195594/6
refs/changes/90/202090/4
refs/changes/92/202092/4
'''
def main ():
gerrit=205739
with open('gerrit_refs.txt', 'r') as f:
# Here we make a list of refs based on the file
gerrit_refs = [line.strip() for line in f]
match = None
for ref in gerrit_refs:
if gerrit in ref:
match = ref
print match
break
if __name__ == '__main__':
main()
Error:-
TypeError: ‘in ‘ requires string as left operand, not in
Filter out matching lines in your list comprehension:
Note the need to change the
gerritsearch variable to a string. Theinoperator doesn’t work with mixed variables.This will work if there are multiple references as it returns a list. If you just want to grab the first match, then simply extract the first item: