Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9046569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:41:20+00:00 2026-06-16T11:41:20+00:00

newbie question,I have the following code where I need to match the data in

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:41:21+00:00Added an answer on June 16, 2026 at 11:41 am

    Filter out matching lines in your list comprehension:

    def main ():
        gerrit = 205739
        gerrit_str = str(gerrit)
        with open('gerrit_refs.txt', 'rb') as f:
            # Here we make a list of refs based on the file
            gerrit_refs = [line.strip() for line in f if gerrit_str in line]
    
        if gerrit_refs:
            # At least one match was found.
            print gerrit_refs
    

    Note the need to change the gerrit search variable to a string. The in operator 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:

    if gerrit_refs:
        print "First match:", gerrit_refs[0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question for you. I have the following code. I get an error because
A newbie question. I have the following piece of Java code: import acm.program.*; import
Quick newbie question. Let's say I have the following code in Vim: void main()
this is a newbie question I have the following code: var total = 0L
Newbie question again... I already have the following code for making links shortened using
I have another newbie Python question. I have the following piece of code that
Newbie question. I have two c# classes - a code class (say, CodeClass) and
quick and very basic newbie question. If i have list of dictionaries looking like
Following up on my previous newbie question about Paypal , I have a new
Newbie question. I have the following models: class Asset < ActiveRecord::Base belongs_to :assetable, :polymorphic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.