简体   繁体   中英

Python: Can I use on the regular expression

I tried to do some work on with regular expression but, I had difficulties as there are lots of list of cars. The line of code will longer if I used the below code

car_name = re.compile(r'.*(?=\n\nBMW I3\s*?\n)')
car_name = re.compile(r'.*(?=\n\nBMW I8\s*?\n)')
car_name = re.compile(r'.*(?=\n\nBMW 320d\s*?\n)')
...

So, I created a CSV file that contains a car list and get the list as following code:

car_df = pd.read_csv("my path")

I wonder how can I use the csv file applied to the regular expression Is ther any way to bring a list of cars and apply on the regular expression?

Try this :

import re

bmw_cars = []
for car in car_list:
   if re.search('.*BMW.*',car):
      bmw_cars.append(car)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM