1

I have a list

list = ['plutino?','res 2:11','Uranus L4','res 9:19','damocloid','cubewano?','plutino']

I want to search every element from the list in a file with the next format and print the line after match

1995QY9      |  1995_QY9     | plutino       |  32929  |                |    39.445  |   0.260  | 29.193    |    49.696   |    4.8   |     66  | #   0.400   |  1.21 BR-U   | ?
1997CU29     |  1997_CU29    | cubewano      |  33001  |                |    43.534  |   0.039  | 41.815    |    45.253   |    1.5   |    243  |             |  1.82 RR     | 
1998BU48     |  1998_BU48    | Centaur       |  33128  |                |    33.363  |   0.381  | 20.647    |    46.078   |   14.2   |    213  | #   0.052   |  1.59 RR     | ?
1998VG44     |  1998_VG44    | plutino       |  33340  |                |    39.170  |   0.250  | 29.367    |    48.974   |    3.0   |    398  | #   0.028   |  1.51 IR     | 
1998SN165    |  1998_SN165   | inner classic |  35671  |                |    37.742  |   0.041  | 36.189    |    39.295   |    4.6   |    393  | #   0.060   |  1.13 BB     | 
2000VU2      |  2000_VU2     | unusual       |  37117  | Narcissus      |     6.878  |   0.554  |  3.071    |    10.685   |   13.8   |     11  | #   0.088   |              | 
1999HX11     |  1999_HX11    | plutino?      |  38083  | Rhadamanthus   |    39.220  |   0.151  | 33.295    |    45.144   |   12.7   |    168  |             |  1.18 BR     | 
1999HB12     |  1999_HB12    | res 2:5       |  38084  |                |    56.376  |   0.422  | 32.566    |    80.187   |   13.1   |    176  |             |  1.39 BR-IR  | 

I am using the next code to do that

for i in list:
with open("tnolist.txt") as f:
    for line in f:
        if re.search(i, line):
            print(line)

The code works fine for all element, except for plutino. When the variable i is plutino the code prints lines for plutino and for plutino?.

4
  • The ? in the string is making an issue. You need a mechanism to make an exact match for that string. You can do ind = i.index('?') ; i = i[:ind] + "\\" + i[ind:] to append \` in the string so that it will take ``? as an actual character in your search. Commented May 28, 2018 at 4:36
  • 1
    Please, do not name list objects as list! Commented May 28, 2018 at 4:53
  • 1
    Do you actually re-open the file for each world in the list? Commented May 28, 2018 at 4:57
  • Finally, how can this code "work fine" when it does not "print the line after match"? Commented May 28, 2018 at 4:58

1 Answer 1

2

This happens because plutino is a substring of plutino?, so the regex parser matches the first part of plutino? and returns a non-falsey answer. Without a whole lot of additional work, you should be able to fix the problem with re.search(i, line+r'\s'), which says that you need to have a whitespace character after the phrase you're searching. As the file gets longer and more complicated, you might have more such exceptions to make the regex behave as desired.

Update: I also like visual regex editors for reasons like this. They make it easy to see what matches and what doesn't.

Another option would be something like i==line.split('|')[2].strip() which extracts the portion of your file you seem to care about. The .strip() method can become inefficient on long lines, but this might fit your use case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.